4

This is my first attempt with Parse on Swift, and I've followed the quick start guide on Parse's website as seen here: https://parse.com/apps/quickstart#parse_data/mobile/ios/swift/existing

Unfortunately I get this error, 'PFObject' does not have a member named 'subscript'. I have followed the example code and instructions exactly as they are written and have redone this several times but with the same result. I really fail to see where I've gone wrong with this particularly as it's straight off the site. I'm on Xcode 6.4 targeting iOS SDK 8.4 using version 1.8.3 of the Parse library.

This is the code on the ViewController in the viewDidLoad method (I have remembered to include import Parse)

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

and this is the code I've added to AppDelegate. Again, I have imported Parse and Bolts as specified in the guide. This is the only code I have changed, the rest is as a standard blank single-view application would be. I have redacted the application ID and client key.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

Parse.enableLocalDatastore()

Parse.setApplicationId("-redacted-",
  clientKey: "-redacted-")

PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
}

Any ideas as to how I might fix this or where I might have gone wrong?

markeh21
  • 189
  • 12

2 Answers2

0

You might wait for a better answer in which somebody explains swift unwrapping and optional types as it relates to parse objects (I wish I was qualified to give such an answer). In the meantime a quick fix is to bypass the object["foo"] sugar, and use the the functional form of property access, like object.objectForKey("foo").

danh
  • 62,181
  • 10
  • 95
  • 136
0

I tried it and have the same result. I also did try to do the

testObject.setValue("bar", forKey: "foo")

And same error occur. Seem like it is inserting "nil" instead of the value or object. I don't why.

What I can suggest though, is use the Objective-C files in Parse.com and then create a bridging header file.

Then do the code provided in Swift language - the one in the AppDelegate.swift and the one with the viewDidLoad.

The code should work.

I think there must be a problem/incompatibility with their new framework for the Swift Language . I'm not sure about it.

Incase the same error occurs, use the

testObject.setValue("bar", forKey: "foo")

in exchange with:

testObject["foo"] = "bar"

Though I read this... I don't know if this is related to the problem here on their starter project: 'PFObject' does not have a member named 'subscript'

Community
  • 1
  • 1
nycdanie
  • 2,921
  • 3
  • 18
  • 21