Subclassing PFObject in Swift

前端 未结 5 2301
南方客
南方客 2020-12-05 14:49

The Parse documentation for adding properties and methods on PFObject subclasses conveniently skips the Swift syntax in their sample code listing just the Objective-C syntax

5条回答
  •  醉话见心
    2020-12-05 15:45

    Swift 1.2

    First: Create a swift file and define the subclass. Don't forget to import Parse! (for example Armor)

    import Foundation
    import Parse
    
    
    class Armor: PFObject, PFSubclassing {
    
        // MARK: - PFSubclassing
    
        override class func initialize() {
            struct Static {
                static var onceToken: dispatch_once_t = 0;
            }
            dispatch_once(&Static.onceToken) {
                self.registerSubclass()
            }
        }
    
        class func parseClassName() -> String {
            return "Armor"
        }
    
    
        // MARK: - Parse Core Properties
    
        @NSManaged var displayName: String?
    
    }
    

    Note: You can define your properties as optionals. Every "undefined" value inside the Parse Core Manager will be translated to "nil".

    Second: Register all subclasses, inside your AppDelegate.swift.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
    
        // MARK: - Parse Setup
        Parse.setApplicationId("YOUR_ID", clientKey: "YOUR_KEY")
    
        // MARK: - Parse Register Subclasses
        Armor.registerSubclass()
    
        return true
    }
    

提交回复
热议问题