user3857868
user3857868

Reputation:

Parse Query Crashes (Swift)

I'm building an iOS app using Swift and Parse. I have a simple data table with three keys. I'm trying to use a Parse query in viewDidAppear to access each element in the table. I'm trying to println() the elements for now, and then later would like to append them to a [String] array.

Here's what I've tried:

var query = PFQuery(className: "BCCalendar")
query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]!, error: NSError!) -> Void in
    if error == nil {
        for object in objects {
            var event = object["events"] as String
            var date = object["dates"] as String
            var formattedDate = object["formattedDates"] as String

            println("Event \(event) is on \(date) which is formatted as \(formattedDate)")
        }
    } else {
        // Do something
    }
}

Every time I run the code, the app crashes with the error: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1008564b0). I've tried multiple methods to query from Parse. I've made sure the table has elements in it. But, I still have the same problem. Any ideas? Thanks!

More details on the crash: It crashes on this line: var event = object["events"] as String. I've added a breakpoint and it does reach the for loop. The object does contain the right elements (I was able to print it somehow before).

Upvotes: 0

Views: 166

Answers (1)

ztan
ztan

Reputation: 6921

Your events is an array of String, so if you want to print the events, you have to do the following:

var events = object["events"] as [String]
for event in events {
    println(event)
}

Upvotes: 1

Related Questions