我正在寻找在一个关键Array
的Dictionarys
,我想结果值转换成Int
数值.这是我试过的.
if let result = lasrIDArray.flatMap( {$0["\(self.selectedTitle)"]} ).first {
print(result)
if let number = result as? NSNumber {
let tag = number.integerValue
let currentScroll = view.viewWithTag(Int(api.selectedCatID)!) as! UIScrollView
let lastImgVw = currentScroll.viewWithTag(tag) as! UIImageView
print(lastImgVw.frame.origin.y)
}
}
Run Code Online (Sandbox Code Playgroud)
但是if let number = result as? NSNumber
没有按预期工作.转换此值的正确方法是什么?
Ana*_*mje 15
我不知道你的代码,但这对你有帮助.
您可以通过这种方式获取AnyObject值...
let data :AnyObject = "100"
let score = Int(data as! String)! //Force Unwrap optional value it will be dengerious for nil condition.
print(score)
Run Code Online (Sandbox Code Playgroud)
或者也尝试这种方式
let hitCount = "100"
let data :AnyObject = hitCount as AnyObject //sometime Xcode will ask value type
let score = Int(data as? String ?? "") ?? 0
print(score)
Run Code Online (Sandbox Code Playgroud)
let hitCount = "100"
let data :Any = hitCount //Any type Value passing here
let score = Int(data as? String ?? "") ?? 0
print(score)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13478 次 |
最近记录: |