2013-12-14 50 views
0

[X-Post from parse.com]PFObject子類的屬性,另一個PFObject子類

Person子類的PFObject

Address也是的子類PFObjectAddressPerson類型的@property

// Person.h 

@interface Person : PFObject<PFSubclassing> 
@property (retain) NSString *firstName; 
@property (retain) NSString *secondName; 
+ (NSString *)parseClassName; 
@end 

// Person.m 

#import <Parse/PFObject+Subclass.h> 

@implementation PP4MEAddress 
@dynamic firstName; 
@dynamic firstName; 
+ (NSString *)parseClassName { 
    return @"Person"; 
} 

// Address.h 

@interface Address : PFObject<PFSubclassing> 

@property (retain) Person *deliverToPerson; 
@property (retain) NSString *line1; 
@property (retain) NSString *line2; 

+ (NSString *)parseClassName; 

// Address.m 
#import <Parse/PFObject+Subclass.h> 

@implementation PP4MEAddress 
@dynamic deliverToPerson; 
@dynamic line1; 
@dynamic line2; 

+ (NSString *)parseClassName { 
    return @"Address"; 
} 

兩個子類被登記在App Delegate

在方法中MyViewController我想設置這些經如此

例如

-(void)myMethod 
{ 

    Address *address = [Address object]; 
    address.deliverToPerson.firstName = @"John"; 
    address.deliverToPerson.secondName = @"Smith"; 
    address.line1 = @"123"; 
    address.line2 = @"Main Street"; 
    [address saveInBackground]; 

} 

當該代碼執行時,Address類的條目是在Parse.com Web應用程序輸入到Data Browser。然而,唯一列objectIdcreatedAtupdatedAtACL

我想這是因爲,在引擎蓋下Person類型的Address @property是不是被allocatedinitialised

如何克服呢?

難道我寫

@property (retain) Person *deliverToPerson; 

Address.m定製settergetter

如果是這樣,我注意到@propertyatomic,因此我需要在上述settergetter中寫入鎖碼嗎?

謝謝

+0

你有機會檢查答案嗎? –

+0

感謝您的答案@MartinR。我沒有接受它的原因是我正在尋找一種不同的方法,並回答關於房產原子性的問題。初始化控制器中的Address屬性,似乎並不遵循MVC設計。這就是爲什麼我在Address中尋找一個自定義setter/getter的想法,它會分配和初始化Person對象;如果這是一個不正確的方法,爲什麼它是一個答案。謝謝 –

回答

2

我不熟悉parse.com,但我強烈認爲你必須 設置關係之前分配Person對象。喜歡的東西

Address *address = [Address object]; 
address.line1 = @"123"; 
address.line2 = @"Main Street"; 

Person *person = [Person object]; 
person.firstName = @"John"; 
person.secondName = @"Smith"; 
address.deliverToPerson = person; 

[address saveInBackground]; 
0

你必須設置你的PFobject 這樣

PFObject *anotherPlayer = [PFObject objectWithClassName:@"Player"]; 
    [anotherPlayer setObject:@"DataValue" forKey:@"Name"];