2

I can´t get the object properties when retrieving an object from Parse Data Browser. This happened after I changed from "@NSManaged var friends" to "dynamic var friends". Even "name" show nil in User.logInWithUsernameInBackground block which is crazy because the login succeeds. The ACL for User is set to "public read".

User object:

class User : PFUser, PFSubclassing {

    dynamic var friends:[User]!
    dynamic var name:String!

     override class func load() {
        self.registerSubclass()
    }
}

Retrieving the User along with the friends. println showing nil

var query = User.query()
query.includeKey("friends")
query.getObjectInBackgroundWithId(currentUser.objectId) {
    (pfObject: PFObject!, error: NSError!) -> Void in
    if pfObject != nil {
        var user = pfObject as User
        var friends = user.friends as [User]
        println("friends: \(friends)") //nil
   } else {
        println(error)
   }
}

Login. println showing nil

User.logInWithUsernameInBackground(USERNAME, password:PASSWORD) {
        (user: PFUser!, error: NSError!) -> Void in
        if user != nil {
            println("Logged in with user: \(user.name)") //nil
        } else {
            println(error)
        }
    }
0xRLA
  • 3,279
  • 4
  • 30
  • 42
  • Why did you switch away from @NSManaged? Just for our reference do you have a link to somewhere that suggests doing that? – chasew Sep 08 '14 at 13:33
  • Thx for replying ccwasden. Maybe I misunderstood but @NSManaged seems to be used only for core data: https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html – 0xRLA Sep 08 '14 at 13:46
  • 1
    According to this post, parse subclasses need @NSManaged http://stackoverflow.com/questions/24581981/subclassing-pfobject-in-swift – chasew Sep 08 '14 at 13:48
  • I read that one too and yes maybe that´s the case then, just seems strange. thx! – 0xRLA Sep 08 '14 at 13:58

2 Answers2

6

Looking a little deeper for you, it seems the hurdle is a misunderstanding of what the dynamic modifier in Swift does. Apparently, dynamic in Swift is used for Key-Value observing, not declaring a variable's accessors to be defined at runtime (what @dynamic does in Objective-C)

See this for a description of dynamic in Swift https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-XID_8

and this for the description of why @NSManaged works the way @dynamic does in Objective-C https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html#//apple_ref/doc/uid/TP40014216-CH5-XID_66

chasew
  • 8,438
  • 7
  • 41
  • 48
0

With xCode 6.1.1 I was able to get this working without the bridging header but I did need to use @NSManaged. Here's how... Just:

import Parse 

at the top of the calling module. For the class declaration .swift file dynamic didn't work so I needed to use @NSManaged for the variable types to get them to link to the Parse class variables successfully. Like this:

class PSCategory : PFObject, PFSubclassing {
    override class func load() {
        self.registerSubclass()
    }
    class func parseClassName() -> String! {
        return "Category"
    }

    @NSManaged var Name: String
}

Then in my query all the names are dynamically linked:

var query = PSCategory.query() // PFQuery(className: "Category")

query.cachePolicy = kPFCachePolicyCacheElseNetwork // kPFCachePolicyNetworkElseCache
query.maxCacheAge = 60 * 60 * 24  // One day, in seconds.
query.findObjectsInBackgroundWithBlock {
    (categories: [AnyObject]!, error: NSError!) -> Void in
    if error == nil {
        for abstractCategory in categories {
            let category = abstractCategory as PSCategory

            NSLog("Category Name: %@", category.Name)
        }
    } else {
        NSLog("Unable to retrieve categories from local cache or network")
    }
}
Timothy Tripp
  • 3,277
  • 1
  • 14
  • 8