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?

fabian789
  • 8,348
  • 4
  • 45
  • 91
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135

8 Answers8

44

I ended up with the ugly line:

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

no warnings, no errors, no runtime error

John Erck
  • 9,478
  • 8
  • 61
  • 71
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Why do you need to type uuidString, won't it be the same if you do var uuidString = regionToMonitor["uuid"] as String! – gerarddp Oct 29 '14 at 14:46
  • in the beta I developed this for, not typing it CRASHED :) – Daij-Djan Oct 29 '14 at 14:47
  • 1
    By the way, if `regionToMonitor` were an optional NSDictionary or Dictionary, Xcode 6.3 would say that AnyObject? cannot be cast to String, which is misleading. – sudo Jun 10 '15 at 19:36
  • @sudo Your comment got me out of 20 minute error search. Indeed, XCode is reporting totally unrelated error, I had to add `regionToMonitor!["blah"]` and tadaaaaa....no errors :) – Igor Pantović Aug 19 '15 at 18:31
11

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.

Amitay
  • 468
  • 5
  • 20
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. :)

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Try removing some of the extra downcasting (`as ...`) you're doing before accessing the dictionary - this might be an issue with inferred types. – rickster Jun 03 '14 at 01:04
  • I'am not completely sure about, what the NSBundle returns here. The first method does indeed work (but not without a warning), the second doesn't (not just because you're missing a parenthesis). – Leandros Jun 03 '14 at 01:08
  • Cmd-click `NSBundle` in your code to see its Swift declarations. (Xcode automagically Swift-ifies the header on demand, with comments.) That might help track down type issues. – rickster Jun 03 '14 at 01:11
  • As for missing parens... clearly someone needs to figure out how to embed SO in a playground or vice versa. :D – rickster Jun 03 '14 at 01:11
  • `var regions = NSBundle.mainBundle().infoDictionary["Regions"]` is AnyObject?, but it contains an NSArray with zero entries. It definitely doesn't work without downcast. – Leandros Jun 03 '14 at 01:12
  • You can't `println` a Dictionary like this, I said it doesn't work, not because of parens. ;) I have no clue why, but it doesn't work. Test it out in the PlayGround. – Leandros Jun 03 '14 at 01:14
2

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.

Leandros
  • 16,805
  • 9
  • 69
  • 108
  • correction: if do this + a println -- the whole thing comes crashing don EXC_INVOP on reading the plist – Daij-Djan Jun 03 '14 at 06:20
1

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.

ACengiz
  • 1,285
  • 17
  • 23
0

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.

Mike Seghers
  • 1,925
  • 2
  • 16
  • 12
  • Doing this causes my swift process to crash with error 254. See http://stackoverflow.com/questions/24154163/xcode-swift-failed-with-exit-code-254 for details. – tng Jul 05 '14 at 04:50
0

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")
}
ggomeze
  • 5,711
  • 6
  • 29
  • 32
-1

Keep it simple:

let uuidString = "\(regionToMonitor["uuid"])"
Scodino
  • 1
  • 1
  • 2
    Welcome to Stack Overflow! I recommend you [take the tour](http://stackoverflow.com/tour). When giving an answer it is preferable to give [some explanation as to WHY your answer](http://stackoverflow.com/help/how-to-answer) is the one. – Stephen Rauch Jan 31 '17 at 01:55