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
Are Built-In Intrinsic Functions Available in Swift 3
Can't Create Default Closure Parameter in Array Extension Method in Swift
Passing Dynamic Int Variable from One Class to Another Class in Swift
Write into Settings Bundle in Swift
Custom Uitabbar Unselected Item's Color
Swift Protocol as Generic Parameter
Gcdasyncsocket Multiple Connections Wont Accept Data from Multiple Sockets
Swiftui Multiline Text Background Color
Distributednotificationcenter - How to Pass Data Between Applications
Swift Vscode Class in Other File Not Recognized
Call Function When If Value in Nsuserdefaults.Standarduserdefaults() Changes
How to Make a Function Complete Before Calling Others in an Ibaction
Avcapturevideopreviewlayer Is Not Visible on The Screenshot
Using Nil-Coalescing Operator with Try? for Function That Throws and Returns Optional
Swiftui - Unwrap Optional Image Data to Create Image Based on Uiimage(Data)
Swift:Pause and Resume Nstimer