Hi all,
im having difficulties saving an nsmutablearray to a plist.
Before adding an object ty my array (services), I set a type for each object as shown below in order to differenciate how they are displayed in my tableView:
Quote:
typedef enum _StringType
{
Input, Received
} StringType;
@interface MyObject: NSObject
{
NSString* theString;
StringType type;
}
@property (nonatomic, retain) NSString* theString;
@property (readonly) StringType type;
-(id) initWithString NSString*) aString andType: (StringType)_type;
@end
@implementation MyObject
@synthesize theString, type;
-(id) initWithString: (NSString*) aString andType: (StringType)_type{
self = [super init];
if( self != nil )
{
self.theString = aString;
type = _type;
}
return self;
}
//also add this in MyObject
-(void)dealloc
{
[theString release];
[super dealloc];
}
@synthesize theString, type;
@end
|
Quote:
- (void)sendAction: (id) sender {
//when something new is input by the user
MyObject* obj = [[MyObject alloc] initWithString:input.text andType: Input];
[services addObject: obj];
[obj release];
[tableView reloadData];
}
|
This is the my code for writing the array to the plist:
Quote:
- (void)applicationWillTerminate UIApplication *)application {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *appDir = [[NSBundle mainBundle] resourcePath];
NSString *dest = [docsDir stringByAppendingPathComponent: @"old.plist"];
[services writeToFile:dest atomically: YES];
NSLog(@"Services array: %@", services);
}
|
And this is how my array looks in the log:
Quote:
2009-09-13 01:48:56.975 Texter[15619:20b] Services array: (
<MyObject: 0xd06510>,
<MyObject: 0xd31a10>,
<MyObject: 0xd8ba50>,
<MyObject: 0xd348f0>
)
|
For some reason, writeToFile: is unable the handle its operation with this array. With any other "normal" array the writing to the plist works perfectly. For example with:
Quote:
|
NSArray *example = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", nil];
|
I guess I would have to convert the array to some other format but I need it like this so that the tableView knows how the arrange its cell content when loading from the plist according to each objects type.
I hope somebody can help me out here!
Thanks alot!