How to Set an Initial Value for @Nsmanaged Property Pfobject Subclass

How to set an initial value for @NSManaged property PFObject Subclass?

var isFavorite: Bool {
get {
if let isFavorite = self["isFavorite"] as? Bool {
return isFavorite
}
return false //default
}
set {
self["isFavorite"] = newValue
}
}

Declare a read-only @NSManaged property in Swift for Parse's PFRelation

Now you should use:

var relatedThings: PFRelation! {
return relationForKey("relatedThings")
}

subclass PFObject and override init

Try updating your subclass to the following. Notice the NSManaged keywords for the Parse properties you've added to your Wish subclass and the removal of super.init()

You will probably also want to look into replacing the UIImage properties with the Parse-provided PFFile properties. You can easily store and load images using PFImageView included in the ParseUI framework.

import Foundation
import Parse

class Wish: PFObject, PFSubclassing {

// MARK: - Parse Core Wish Properties

@NSManaged var id: String?
@NSManaged var name: String?
@NSManaged var descriptionWish: String?
@NSManaged var photo: UIImage
@NSManaged var photoThumbnail: UIImage
@NSManaged var amount: Double?
@NSManaged var amountDonated: Int?

// MARK: - Lifecycle

override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}

static func parseClassName() -> String {
return "Wish"
}

convenience init(id: String?, name: String?, descriptionWish: String?, photo: UIImage, photoThumbnail: UIImage, amount: Double?, amountDonated: Int?) {
self.init()
self.id = id
self.name = name
self.descriptionWish = descriptionWish
self.photo = photo
self.photoThumbnail = photoThumbnail
self.amount = amount
self.amountDonated = amountDonated
}
}

Is it possible to override getters and setters for @dynamic properties in an NSManagedObject subclass?

Never call [super valueForKey:..] in a NSManagedObject subclass! (unless you implemented them in a superclass yourself)
Instead use the primitive accessor methods.

Sample getter/setter implementation from ADC:

@interface Department : NSManagedObject
@property(nonatomic, strong) NSString *name;
@end

@interface Department (PrimitiveAccessors)
- (NSString *)primitiveName;
- (void)setPrimitiveName:(NSString *)newName;
@end

@implementation Department

@dynamic name;

- (NSString *)name
{
[self willAccessValueForKey:@"name"];
NSString *myName = [self primitiveName];
[self didAccessValueForKey:@"name"];
return myName;
}

- (void)setName:(NSString *)newName
{
[self willChangeValueForKey:@"name"];
[self setPrimitiveName:newName];
[self didChangeValueForKey:@"name"];
}

@end


Related Topics



Leave a reply



Submit