Swift, Parse and Xcode 6 beta6

My request to Parse now throws a quick compiler error in Xcode 6 beta6 (see error below). It used to work well (and my example is simple and comes from the Parse documentation). I changed one of Xcode 6 beta 6: from "objects: AnyObject []!" to objects: [AnyObject]! "(due to the error" Array types are now written with parentheses 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 explicitly downcast to "PFObject", did you mean to use "like" 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 : PFObject does not conform to SequenceType

And if I change objects: [AnyObject]! by objects: [PFObject]! I am getting 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 CORRECT THE OUTPUT OF THE COMPUTER

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

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

      

UPDATED CORRECT ANSWER for runtime

The above answer fixed the compiler issue, not the execution. After talking with Parse parsers, their documentation is not updated, as beta 6 is missing. To encode the PFObjects returned from a query, simply do "for object in objects {}":

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

      

+3


source to share


1 answer


You are trying to downgrade an array which I believe. What happens if you change this:

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

      

For this:

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

      

I also wanted to point out that this may not do what you intend:



if !(error != nil) {

      

The extra exclamation mark preceding the parenthesis creates double negative that can make your intentions ambiguous.

UPDATE

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

for object in objects {}

      

+2


source







All Articles