Cannot cast NSDictionary to Dictionary

(Marko Havu) #1

Is NSDictionary not compatible with Dictionary in Silver:

import Foundation

let path = "Defaults.plist"
let userDict = NSDictionary(contentsOfFile: path) as? [String: AnyObject]
// E: Cannot cast from "NSDictionary<id!,id!>!" to "Swift.Dictionary<String,AnyObject>?"
0 Likes

(marc hoffman) #2

On popular request, Swift Dictionary and Array are not structs, as in Apple Swift, not classes, NSDictionary is a class. You should be able to convert from one to the other, but they are no longer a direct mapping. I checked, and there are cast operators that should allow you to cast between them, but I’m guessing the problem why the above cast fails are the mismatched generic types (id/id vs String/AnyObject)

0 Likes

(Marko Havu) #3

AnyObject/AnyObject does not work either. Shouldn’t that match id/id?

0 Likes

(marc hoffman) #4

Hm, yeah, that should work (but, reproduced, it doesn’t). logging.

0 Likes

(RemObjects) #5

Thanks, logged as bugs://81824

0 Likes

(marc hoffman) #6

Latest SBL GitHub commit should have cast operators to let NSArray and NSDictionary be cast from/to [T] and [T:U] with mismatched generic types. I’d appreciate if you can let me know if this works for you.

0 Likes

(Marko Havu) #7

Doesn’t seem to work. Can libToffee mask the SBL? (I’ve removed libSwift from the compiler directory.)

0 Likes

(marc hoffman) #8

It shouldn’t.

the .a as well? otherwise you might be linking against the old, installed, binary, even if your project references the new .fx (Apple’s linker is a bit daft).

—marc

0 Likes

(Marko Havu) #9

Yes, both.

0 Likes

(marc hoffman) #10

Hm ok. can you send me a quick testcase?

0 Likes

(Marko Havu) #11
import Foundation

let someObject = Date()
let nsdict: NSDictionary = ["anObject": someObject,
                            "helloString": "Hello, World!",
                            "theAnswer": 42]
let dict1 = nsdict as? [String: AnyObject]  // Invalid cast
let dict2 = nsdict as? [AnyObject: AnyObject]  // Invalid cast
print(dict1)
1 Like

(marc hoffman) #12

thanx, i’ll have a look.

0 Likes

(RemObjects) #13

Thanks, logged as bugs://81978

0 Likes

(marc hoffman) #14

Reproduced, thanx. this looks like it’s a compiler issue…

0 Likes

(RemObjects) #15

bugs://81978 got closed with status fixed.

0 Likes

(Marko Havu) #16

I guess this didn’t make it to Fire 10.0.0.2377, since the above test case still doesn’t work. Will there be a new build this week?

0 Likes

(marc hoffman) #17

hmm this was supposed to be fixed back when it was closed. if it still doesn’t work, we’ll have to check. there’ll be a new build today, yes, but it won’t have any changes with regard to this issue, sorry :(.

0 Likes

(RemObjects) #18

bugs://81978 got reopened.

0 Likes

(RemObjects) #19

bugs://81978 got closed with status fixed.

0 Likes

(RemObjects) #20

bugs://81824 got closed with status fixed.

0 Likes