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>?"
(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)

(Marko Havu) #3

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

(marc hoffman) #4

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

(RemObjects) #5

Thanks, logged as bugs://81824

(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.

(Marko Havu) #7

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

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


(Marko Havu) #9

Yes, both.

(marc hoffman) #10

Hm ok. can you send me a quick testcase?

(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
1 Like
(marc hoffman) #12

thanx, i’ll have a look.

(RemObjects) #13

Thanks, logged as bugs://81978

(marc hoffman) #14

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

(RemObjects) #15

bugs://81978 got closed with status fixed.

(Marko Havu) #16

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

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

(RemObjects) #18

bugs://81978 got reopened.

(RemObjects) #19

bugs://81978 got closed with status fixed.

(RemObjects) #20

bugs://81824 got closed with status fixed.