1
votes

I am quite new to swift and got a pretty weird compiler error:

Command /Applications/Xcode6-Beta5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 254

The Error occours when I ad the following lines to my code:

var response = HoopsClient.instance().collections["posts"]

response = response["_id"]

when I remove the second line everything compiles fine. I don't get it ^^

edit: The reason is probably that "response" is of type "AnyObject" according to the compiler... but shouldn't that be detected by xcode or give me a runtime error instead of this compiler error?

2
Have you tried to assign to a new variable instead of "response"? - zisoft
yes, tried it, same error. I also tried different indizes (although it would be weird if thats the reason)... - dburgmann
an AnyObject does not have a subscript defined, so that explains why you are getting the compiler error. This is the correct behavior; you don't want it waiting until runtime when it can tell you about the error at compile time. - jacobhyphenated

2 Answers

0
votes

Try casting response as the type you're expecting. From what you're trying to do, instance().collections I would assume that it should return some type of dictionary.

var response = HoopsClient.instance().collections["posts"] as NSDictionary

That way, response now can handle subscripts so you could now (in theory) do:

response = response["_id"]

However

The error you get is regarding bad access to an array. This makes me think that instance().collections actually returns an array of some sort, containing Post objects.

Arrays in Swift can only handle Integer subscripts. If you want to access the information of a Post in the array, you can do something like this:

for post: Post in response {
    println(post._id)
}

I know this is a long shot, but hope it helps.

0
votes

Swift tends to throw error when it cant infer the type of an object, what you could probably do is add a conditional cast as follows

Im assuming that HoopsClient.instance().collections["posts"] is either a Dictionary or an Array

var response = HoopsClient.instance().collections["posts"]
if response is NSArray {
    let item = response.objectAtIndex(0)
    let reponseId: Post = item    
}
if response is NSDictionary {
    let item = response.objectForKet("_id")
    let reponseId: Post = item    
}

Any way, in my experience you should try to cast your variables when assigning from types that return AnyObject, xcode doesn't handle very well type inferring and when it's unable to infer the type the interface starts to throw error, like text editor uncoloring the code.