3

My query to Parse now raises a swift compiler error in Xcode 6 beta6 (see error below). It was working fine previously (and my example is simple, and comes from Parse's documentation). I've changed one thing coming from Xcode 6 beta 6: from "objects: AnyObject[]!" to "objects: [AnyObject]!" (due to error "Array types are now written with the brackets around the element type")

query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if !(error != nil) {
            NSLog("Successfully retrieved \(objects.count) objects.")
            for object : PFObject! in objects { ... } ...

// ERROR: Type [AnyObject] cannot be implicitely downcast to 'PFObject', did you mean to use 'as' to force downcast?

And if I force the downcast as suggested by the previous error, I get another error:

for object : PFObject! in objects as PFObject {
       ...
}

// ERROR: Type PFObject does not conform to protocol SequenceType

And if I change objects: [AnyObject]! by objects: [PFObject]! I get the following error:

query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]!, error: NSError!) -> Void in
        if !(error != nil) {
            for object : PFObject! in objects {

// ERROR: AnyObject is not identical to PFObject

ANSWER TO FIX THE COMPILER ISSUE

Correct answer is below (Xcode suggested the downcast to PFObject while the downcast is on "objects", an array):

for object : PFObject! in objects as [PFObject] {
       ...
}

UPDATED CORRECT ANSWER for execution time

The above answer was fixing the compiler issue, not the execution. After chatting with Parse guys, their documentation is not up-to-date since beta 6 is out. To loop on PFObjects objects returned from a query, simply do a "for object in objects {} ":

 query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]!, error: NSError!) -> Void in
        if (error == nil) {
           for object in objects {
            ...
            } ...
 }
  • do [PFObject]! instead of [AnyObject]! – A'sa Dickens Aug 27 '14 at 14:09
  • In that case, I get the following error: *AnyObject is not identical to PFObject* on the row of *query.findObjectsInBackgroundWithBlock {* – Tom - Lunabee.com Aug 27 '14 at 14:12
  • Oh i see, is there some protocol that must be used in order to use AnyObject as a type? – A'sa Dickens Aug 27 '14 at 14:13
  • i would neglect the loop until you are getting objects, then the loop would be super easy. – A'sa Dickens Aug 27 '14 at 14:15
  • A'sa: I don't know. This piece of code for an async Parse query comes from the Parse documentation (and it was working on previous Xcode 6 betas...) – Tom - Lunabee.com Aug 27 '14 at 14:16
  • basically what that is saying, because === is the identical to operator, that PFObject === AnyObject is false, which is true, so it's expecting you to have AnyObject as an array at that point but parse expects to return a type of PFObject? it should be [PFObject]! indicating an array... are you querying for 1 object? like explicitly first item or last item? – A'sa Dickens Aug 27 '14 at 14:17

1 Answers1

2

You're trying to downcast an array I believe. What happens if you change this:

for object : PFObject! in objects as PFObject {
       ...
}

To this:

for object: PFObject in objects as [PFObject] {
       ...
}

I also wanted to point out that this might not do what you're intending:

if !(error != nil) {

The additional exclamation point preceding the parenthesis is creating a double negative which can make your intentions ambiguous.

UPDATE

As pointed out in comments, Parse suggests to do a simple for-in loop without any explicit downcasting.

for object in objects {}
Logan
  • 49,229
  • 19
  • 92
  • 123
  • You're right, my bad: Xcode suggested me the downcast without the brackets. Indeed, this is "objects", an array, that I'm downcasting... – Tom - Lunabee.com Aug 27 '14 at 14:23
  • It was fixing my compilation issue, not the execution. I was having a *EXC_BAD_ACCESS* on the App Delegate. After chatting with Parse guys, the beta 6 had an impact on Parse for Swift users. The documentation is not up-to-date: we need to loop on objects with for object in objects {...} – Tom - Lunabee.com Aug 27 '14 at 17:30
  • Thanks for the update Tom! That's good info, I'll add it to the answer. – Logan Aug 27 '14 at 17:37