[英]xcode swift AnyObject to Swift convert
let dataJSON = response .dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let decodedJson = NSJSONSerialization.JSONObjectWithData(dataJSON!, options: nil, error: nil) as NSDictionary
println(decodedJson["UserId"])
let BlockedValue = decodedJson["UserId"] as NSString
NSLog("USER ID : %@",BlockedValue)
This code not work when i build code part; 当我构建代码部分时,此代码不起作用;
First println
work output : Optional(1269) But second NSLog not work 第一个
println
工作输出:可选(1269),但是第二个NSLog不工作
How can i convert AnyObject to String i research on google this topic i found some code part but these are not work 我如何将AnyObject转换为String我在Google上研究了这个主题,我发现了一些代码部分,但这些都不起作用
I found this one is not worked 我发现这是行不通的
if let receiver = decodedJson["UserId"] as? NSString {
self.receiver = receiver
}
Any idea help please ? 有任何想法帮助吗?
It seems that decodedJson["UserId"]
is an optional number. 似乎
decodedJson["UserId"]
是一个可选数字。
One way to do it (you have to be sure decodedJson["UserId"] isnt' nil): 一种方法(您必须确保encodedJson [“ UserId”]不为nil):
let number = decodedJson["UserId"]
if number != nil {
let string = "\(number!)"
}
Or in condensed state: 或处于压缩状态:
if let number = decodedJson["UserId"] {
let string = "\(number)"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.