3

In my application i have an custom NSObject, which contains 2 mutable Arrays. I need to save this custom NSOBject into a core data entity, but i have no real idea how i can accomplish that...

After some searching, i found out, that the best way would be to convert the nsobject to nsdata and save it in an transformable field of the entity... but i m not sure how to do that. can someone help me?

heres to code for my custom object:

MeasureData.h

@interface MeasureData : NSObject{
}

@property (nonatomic, strong) NSMutableArray *questionsData;
@property (nonatomic, strong) NSMutableArray *answersData;

- (id) init;

@end

MeasureData.m

#import "MeasureData.h"

@implementation MeasureData

@synthesize questionsData;
@synthesize answersData;

#pragma mark -
#pragma mark int

- (id)init
{
self = [super init];

// Initalize questions array (width data from plist)
questionsData = self.makeQuestionsArray;
// NSLog(@"loaded questions array: %@",questionsData); // debug


// Initalize answers array
answersData = self.makeAnswersArray;
// NSLog(@"loaded answers array: %@",answersData); // debug


return self;
}

-(NSMutableArray *)makeQuestionsArray
{
// Initalize questions array (width data from plist)
NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"questions.list" ofType:@"plist"];
NSMutableArray *questions = [NSMutableArray arrayWithContentsOfFile:path];


/*
 [questionsData insertObject:(NSString *)string atIndex:0];
 */

return questions;
}

-(NSMutableArray *)makeAnswersArray
{
// Initalize answers array
NSMutableArray *answers = [NSMutableArray arrayWithCapacity:0];

return answers;
}

-(id)initWithCoder:(NSCoder *)decoder {
if ((self=[super init])) {
    questionsData = [decoder decodeObjectForKey:@"questionsData"];
    answersData = [decoder decodeObjectForKey:@"answersData"];
}
return self;
}

-(void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:questionsData forKey:@"questionsData"];
[encoder encodeObject:questionsData forKey:@"questionsData"];
}

@end

According to the first comment, i implemented the encoder/coder functions for my custom class. And tried to archive and encode it (i m new to ios, so it could be completly wrong) - but it dont work... can someone tell me whats wrong?

heres the encoding (which dont work XD):

NSMutableData *dataToSave = (NSMutableData *)self.measureData;
NSKeyedArchiver *archiverForData = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataToSave];

[archiverForData encodeObject:dataToSave forKey:@"dataToSave"];
[archiverForData finishEncoding];
//

//theMeasure is the CoreData Entity
theMeasure.result = dataToSave;
jacksbox
  • 911
  • 1
  • 11
  • 24
  • what is self.measureData type? You should create a new, empty NSMutableData object instead of casting your measureData ivar (I guess it will be MeasureData type) and use encodeObject:self.measeureData on your original object. Also note, you copy-pasted 2 times the same line in encodeWithCoder. – MrTJ Mar 19 '12 at 14:28
  • thank you ;) i know use a slightly different approach (maybe its the same, i m not sure) => i take my object and use "NSData *dataToSave = [NSKeyedArchiver archivedDataWithRootObject:measureData];" to convert it to NSData -> this NSData can be stored in a binary attribute and can be reconvertet via NSKeyUnarchiver... pretty easy, but took me about 4 houres trying and trying. – jacksbox Mar 19 '12 at 16:43
  • yes, this way I meant originally! Don't sorry for that 4h, I am sure you studied a lot ;) PS: if you think so, you can also "accept" my answer by clicking on the white ticker under the +/- counter. – MrTJ Mar 19 '12 at 16:46
  • ^^ oh yes - i learned a lot ;) thank you for your answere - it lead me the way XD – jacksbox Mar 19 '12 at 16:56

1 Answers1

2

In outline:

  • create a NSMutableData
  • create a NSKeyedArchiver with initForWritingWithMutableData over your data
  • serialize your arrays / objects / whatever you need (that implements NSCoding) with encode... methods of NSCoder
  • create a managed object with a BLOB (binary data) type field
  • write your encoded data from the mutable data to this field of the managed object.

In my answer to this question you can find some useful links: NSCoding VS Core data

Community
  • 1
  • 1
MrTJ
  • 13,064
  • 4
  • 41
  • 63
  • hm, tank you, that sounds good - i also tried your suggestion, but i cant make it work - i added my tries to my question, so maybe u could have a look and give me a hint what to do or what i m doing wrong? – jacksbox Mar 19 '12 at 14:10