17
votes

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?

8
NSString == String in Swift. - Leandros
thanks thats what the IDE says too ;) - thats what I thought but this doesn't work -- see my EDIT - Daij-Djan

8 Answers

44
votes

I ended up with the ugly line:

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

no warnings, no errors, no runtime error

11
votes

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
votes

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. :)

2
votes

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.

1
votes

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.

0
votes

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.

0
votes

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")
}
-1
votes

Keep it simple:

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