如何将AnyObject类型转换为Swift ?

时间:2022-07-04 16:32:23

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.

我正在字典数组中搜索一个键,我想将结果值转换为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)
    }
}

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

但是如果让数字=结果?NSNumber没有按预期工作。转换这个值的正确方法是什么?

3 个解决方案

#1


11  

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

我不知道你的密码,但这对你有帮助。

You can get your AnyObject value in this way...

您可以通过这种方式获取AnyObject值……

let data :AnyObject = "100"
let score = Int(data as! String)! //Force Unwrap optional value it will be dengerious for nil condition.
print(score)

Or try this way also

或者也试试这个方法

let hitCount = "100"
let data :AnyObject = hitCount as AnyObject //sometime Xcode will ask value type
let score = Int(data as? String ?? "") ?? 0
print(score)

Output -

如何将AnyObject类型转换为Swift ?


Swift 3.1 & Swift 4

let hitCount = "100"
let data :Any = hitCount //Any type Value passing here 
let score = Int(data as? String ?? "") ?? 0
print(score)

Output -

如何将AnyObject类型转换为Swift ?

#2


0  

Here i have given example to convert Anyobject to Int and String

这里我给出了将Anyobject转换为Int和String的示例

var idInt : Int = 0
if let ids: AnyObject = responseDict["id"] {
    if let idsInt = ids as? Int{
        idInt  = idsInt
        print(idInt)
    }
}


var nameString : String = ""
    if let name: AnyObject = responseDict["name"] {
        if let nameStr = name as? String{
            nameString  = nameStr
            print(nameString)
        }
    }

#3


0  

If your data is decoded from JSON string, it will be decoded as NSNumber or NSString.

如果您的数据是从JSON字符串解码的,那么它将被解码为NSNumber或NSString。

You can use this function:

你可以使用这个功能:

func intValueForKey(key: String, inDictionary dictionary: [String: AnyObject]) throws -> Int {
    if let value = dictionary[key]?.integerValue {
        return value
    } else {
        throw NSError(domain: "Invalid key", code: -1, userInfo: nil)
    }
}

#1


11  

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

我不知道你的密码,但这对你有帮助。

You can get your AnyObject value in this way...

您可以通过这种方式获取AnyObject值……

let data :AnyObject = "100"
let score = Int(data as! String)! //Force Unwrap optional value it will be dengerious for nil condition.
print(score)

Or try this way also

或者也试试这个方法

let hitCount = "100"
let data :AnyObject = hitCount as AnyObject //sometime Xcode will ask value type
let score = Int(data as? String ?? "") ?? 0
print(score)

Output -

如何将AnyObject类型转换为Swift ?


Swift 3.1 & Swift 4

let hitCount = "100"
let data :Any = hitCount //Any type Value passing here 
let score = Int(data as? String ?? "") ?? 0
print(score)

Output -

如何将AnyObject类型转换为Swift ?

#2


0  

Here i have given example to convert Anyobject to Int and String

这里我给出了将Anyobject转换为Int和String的示例

var idInt : Int = 0
if let ids: AnyObject = responseDict["id"] {
    if let idsInt = ids as? Int{
        idInt  = idsInt
        print(idInt)
    }
}


var nameString : String = ""
    if let name: AnyObject = responseDict["name"] {
        if let nameStr = name as? String{
            nameString  = nameStr
            print(nameString)
        }
    }

#3


0  

If your data is decoded from JSON string, it will be decoded as NSNumber or NSString.

如果您的数据是从JSON字符串解码的,那么它将被解码为NSNumber或NSString。

You can use this function:

你可以使用这个功能:

func intValueForKey(key: String, inDictionary dictionary: [String: AnyObject]) throws -> Int {
    if let value = dictionary[key]?.integerValue {
        return value
    } else {
        throw NSError(domain: "Invalid key", code: -1, userInfo: nil)
    }
}