1
votes

I've just updated to Xcode 6 beta 6 and I have a following problem with my code which was working before

let reminderSubject = reminder["subject"]
println("reminderSubject: \(reminderSubject)")
let tempTuple = (reminderSubject as String)

reminderSubject is of type AnyObject?! when I try to downcast it to String it crashes with "Swift dynamic cast failure" message. The value printed by println is: Optional(Hey its Test!)

So the question is how to correctly cast AnyObject?! to String in Swift?

1
Typically you'd want to check to see if there actually was an item with that key in your dictionary, and therefore use a path like if let reminderSubject = reminder["subject"] { ... }, which would execute only if your reminder had a subject, and make reminderSubject non-optional. You may want to re-think your approach here; if you start seeing lots of exclamation marks it's a bit of a clue you're not writing particularly Swifty code, in my opinion. - Matt Gibson
@MattGibson thanks for your insight - I'll take a look at this since my current solution is "ugly as hell" even for me ;) - pJes2

1 Answers

1
votes

Ok I've done it with:

let tempTuple = ("\(reminderSubject!!)")