"PFObject" does not have a member named "index",

I understand this particular error has already been posted here and there, and the code is somewhat basic, but I myself still can’t figure it out, and I need some advice.

The fact is that I add the first two lines of code provided on parse.com to save objects

var gameScore = PFObject(className:"GameScore") gameScore["score"] = 1337 

I get the following error for the second line:

'PFObject' has no member named 'subscript'

I am on Xcode 6.3 beta 2. All the required libraries are related to the binary, <Parse/Parse.h> , imported through BridgeHeader.

What syntax should I use?

+7
ios xcode swift
source share
5 answers

This is due to the parse sdk syntax version 1.6.4, which added annotations for Nullability to the Objective-C framework. In particular, the Parse/PFObject.h defines:

- (PF_NULLABLE_S id)objectForKeyedSubscript:(NSString *)key;

which causes a Swift compilation error. Removing PF_NULLABLE_S fixes the problem.

On the other hand, it seems correct that the object for the key index may be nil , so I suspect it is a Swift error ...

+10
source share

The problem is changing the signature of the method, as Kashif suggested. Swift does not seem to be able to switch to the Objective-C method because the signature no longer matches the names of the subscripts.

Workaround 1

You can get around this without changing the structure by calling the substring method directly, instead of using the [] operator:

Instead of using the instructions below to get the value of a specific key:

 let str = user["key-name"] as? Bool 

Please use the following instructions:

 let str = user.objectForKey("key-name") as? Bool 

and

Instead of using the instructions below to set the value of a specific key:

 user["key-name"] = "Bla bla" 

Please use the following instructions:

 user.setObject("Bla bla", forKey: "key-name") 

Workaround 2

Another solution is to add an extension to PFObject that implements the subscript element and calls setValue:forKey: ::

 extension PFObject { subscript(index: String) -> AnyObject? { get { return self.valueForKey(index) } set(newValue) { if let newValue: AnyObject = newValue { self.setValue(newValue, forKey: index) } } } } 

Note that this second workaround is not entirely safe, since I'm not sure how Parse implements substring methods (maybe they do more than just call setValue:forKey ), it worked in my simple test cases, so it seems like A valid workaround until it is committed to Parse / Swift.

+6
source share

I have successfully executed your exact code.

First, make sure that you really save the object in the background after you set the new value:

 gameScore.save() 

I would double check for spelling errors in the name and subclass of the class; if they are incorrect, this will not work.

If this is not a problem, check in Parse that the subclass "score" is set to a number. If you accidentally set it as a string, setting it as a whole will not work.

If these suggestions did not get into the solution, I'm not sure what the problem is. Hope I helped.

+1
source share

I encountered a similar error with Parse 1.6.4 and 1.6.5 in the header file PFConstants.h with the method parameters.

Xcode 6.3 beta 4 has a note in the "New in ..." section regarding operators with a null value.

Moving the nullability operator between the pointer operator and the variable name corresponds to / compilation.

Change:

 PF_NULLABLE_S NSError *error 

in

 NSError * PF_NULLABLE_S error (ie, NSError* __nullable error) 

... resolved a compiler error.

This also worked for block parameters defined with id. So that...

 PF_NULLABLE_S id object 

becomes:

 id PF_NULLABLE_S object 

In the above case, it is possible:

 - (id PF_NULLABLE_S)objectForKeyedSubscript:(NSString *)key; 

Ie, the nullability operator is after the pointer type.

0
source share

I know this has been a while, but I had a similar problem when I launched my first Parse application with SDK version 1.9.1.

The Quickstart manual shows the code below as an example of how to save values:

  let testObject = PFObject(className: "TestObject") testObject["foo"] = "bar" testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in println("Object has been saved.") } 

But the error 'PFObject' does not have a member named 'subscript' appeared in the line testObject["foo"] = "bar" . I was able to solve the problem by changing the line to testObject.setValue("bar", forKey: "foo") . (As suggested by the YouTube tutorial video: https://www.youtube.com/watch?v=Q6kTw_cK3zY )

0
source share

All Articles