objective c - iOS - Core Data - Saving an object that contains an array of Objects -
i'm getting started core data, (and i'm trying use magical record). i'm creating pretty simple payment tracking app.
i save payment object has array of debtors. payment object looks like
@class debtor; @interface payment : nsobject @property (strong, nonatomic) nsstring *paymentamountstring; @property (strong, nonatomic) nsstring *titlestring; @property (strong, nonatomic) nsarray *debtorsarray; @property (strong, nonatomic) nsdate *duedate; @property (strong, nonatomic) nsstring *notesstring; @end
and debtorsarray array of debtor objects
@interface debtor : nsobject @property (strong, nonatomic) nsstring *namestring; @property (strong, nonatomic) nsstring *amountowedstring;
how should go saving object since contains array. need create 2 different entities, relationship between payment , debtor? how do this, , how ensure fetched properly?
create 1 entity payment
. have use 'transformable' data type attribute debtorsarray
within entity.
then implement following methods in debtor
class:
- (void)encodewithcoder:(nscoder *)acoder{ [acoder encodeobject:self.namestring forkey:@"namestring"]; [acoder encodeobject:self.amountownedstring forkey:@"amountownedstring"]; } -(id)initwithcoder:(nscoder *)adecoder{ if(self = [super init]){ self.namestring = [adecoder decodeobjectforkey:@"namestring"]; self.amountownedstring = [adecoder decodeobjectforkey:@"amountownedstring"]; } return self; }
entity should fetched other fetch query.
hope helps.
Comments
Post a Comment