0

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

12
  • What line does it crash on? Do you have more details for the error? Do you have an exception breakpoint turned on? Have you tried putting in a breakpoint and checking whether it reaches the block / whether the object contains what you expect it to contain? Commented Apr 3, 2015 at 16:20
  • @shim I added more details to my question. Commented Apr 3, 2015 at 16:23
  • So the object contains a value for the key "events" ? Is it actually a String? Commented Apr 3, 2015 at 16:24
  • @shim It's an array key. I'm trying to access each String element in the array. Commented Apr 3, 2015 at 16:24
  • 3
    You have to do object["events"] as [String] if your events is an array of strings Commented Apr 3, 2015 at 16:25

1 Answer 1

1

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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.