Get from AnyObject(NSString) to String

倾然丶 夕夏残阳落幕 提交于 2019-11-29 00:59:17

问题


I am reading a plist key (NSArray with n NSDictionaries):

    let regionsToMonitor = NSBundle.mainBundle().infoDictionary["Regions"] as Array<Dictionary<String,AnyObject>>

now I iterate over it:

    for regionToMonitor in regionsToMonitor {

and now I want to to get uuidString of the regionToMonitor

in ObjC: NSString *uuidString = regionToMonitor[@"uuidString"];

in swift I try: let uuidString = regionToMonitor["uuid"]!.stringValue;

the above does compile but the string is always nil in swift. regionToMonitor["uuid"] when used without !.stringValue works fine in println

how do I get a valid Swift.String here?

I am trying to pass it to NSUUID!


I also tried

let uuidString:String = regionToMonitor["uuid"]
=> AnyObject isn't convertible to String

let uuidString = regionToMonitor["uuid"] as String
=> Could not find an overload for 'subscript' that accepts the supplied arguments

let uuidString = regionToMonitor["uuid"];
=> 'AnyObject?' cannot be implicitly downcast to 'String'; did you mean to use 'as' to force downcast?


回答1:


I ended up with the ugly line:

var uuidString:String = regionToMonitor["uuid"] as! String

no warnings, no errors, no runtime error




回答2:


I found this to work for me

var uuidString: String? = regionToMonitor["uuid"] as AnyObject? as? String

EDIT: this was the answer for an older swift version

Please use the accepted answer.




回答3:


AnyObject? is an optional, because the dictionary may or may not contain a value for the "uuid" key. To get at an optional's value, you have to unwrap it. See Optionals in the documentation.

The safest way to deal with an optional is to put it in a conditional statement.

if let uuidString = regionToMonitor["uuid"] {
    // do something with uuidString
}

If you're absolutely positively sure the dictionary will always contain this key/value pair, you can use an implicitly unwrapped optional (the ! suffix):

println("UUID: \(regionToMonitor["uuid"]!)")       

In this case, if there's no value for the key your app will crash.

If you use ! a lot, it looks like you're yelling all the time... which might help illustrate why you should use it sparingly, if at all. :)




回答4:


I've found a working solution, which compiles without warnings and such:

var regions = NSBundle.mainBundle().infoDictionary["Regions"] as Array<Dictionary<String, AnyObject>>

for region in regions {
    let dict: NSDictionary = region
    var uuid = dict["uuidString"] as String
}

The infoDictionary from the NSBundle returns an NSArray and NSDictionary, not a Swift.Array or Swift.Dictionary. Though, they should be interchangeable, but maybe they aren't as we though.




回答5:


I am not sure my solution is effective of not but here it is.

var uuidVar = regionToMonitor["uuid"]
var uuidString:String = "\(uuidVar)"

Hope it helps.




回答6:


You can also use

var uuidString = regionToMonitor["uuid"]? as String

It has the same results as what you are doing, but is IMHO more clear in intent. The as operator force unwraps anyway, so putting the exclamation mark behind it feels redundant. Putting the question mark behind the dictionary subscript makes it clear you are chaining an optional.




回答7:


If you are sure you want the unwrapped value you can use any of these:

var uuidString:String! = regionToMonitor["uuid"]
var uuidString = regionToMonitor["uuid"] as String!

or even this:

if var uuidString = regionToMonitor["uuid"] {
    println("\(uuidString) has been unwrapped")
}



回答8:


Keep it simple:

let uuidString = "\(regionToMonitor["uuid"])"


来源:https://stackoverflow.com/questions/24005300/get-from-anyobjectnsstring-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!