Irrd Irrd - 4 months ago 8
iOS Question

How to convert AnyObject type to Int in Swift

I am searching a key in an

Array
of
Dictionarys
and I want to convert the result value into an
Int
value. This is what I tried.

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)
}
}


But
if let number = result as? NSNumber
doesn't work as expected. What is the correct way to convert this value?

Answer

I don't know your code but this will be helpful for you.

You can get your AnyObject value in this way..

let data :AnyObject = "100"
let score:Int = Int(data as! String)!
print(score)

Or try this way also

let hitCount:String = "100"
let data :AnyObject = hitCount
let score:Int = Int(data as! String)!
print(score)

your output will come same here.. enter image description here

Comments