Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2009, 07:59 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default Difficulties writing NSMutableArra to plist!

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) initWithStringNSString*) 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)applicationWillTerminateUIApplication *)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!
freshking is offline   Reply With Quote
Old 09-12-2009, 08:46 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

You can only write property list objects to a file and get a plist: NSString, NSData, NSArray, or NSDictionary

You can't have custom objects in the array. If you want to write it out as a plist, you need to make it an array of dictionary object, with strings or whatever you need in the dictionaries.

joe
FlyingDiver is offline   Reply With Quote
Old 09-12-2009, 08:52 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Then how would you recommend storing this data so that it can also be read?
freshking is offline   Reply With Quote
Old 09-12-2009, 08:57 PM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by freshking View Post
Then how would you recommend storing this data so that it can also be read?
What I said above. Instead of an array of MyObject objects, make it an array of NSDictionary objects. You'll have two entries in each dictionary, one being the string (an NSString), and the other the type (string or value).

joe
FlyingDiver is offline   Reply With Quote
Old 03-03-2010, 02:01 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
Default Please help

Where am i going wrong? please help


myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];

NSArray *keys = [NSArray arrayWithObjects:@"Date","Time","Address","City"," State","Zipcode",nil];

[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:@"%@",txtDate.text],

[NSString stringWithFormat:@"%@",txtTime.text],

[NSString stringWithFormat:@"%@",txtAddress.text],

[NSString stringWithFormat:@"%@",txtCity.text],

[NSString stringWithFormat:@"%@",txtState.text],

[NSString stringWithFormat:@"%@",txtZip.text],nil]forKeys:keys]];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myPrimaryinfo.pli st"];
[myPrimaryinfo writeToFileath atomically:YES];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);


Quote:
Originally Posted by FlyingDiver View Post
What I said above. Instead of an array of MyObject objects, make it an array of NSDictionary objects. You'll have two entries in each dictionary, one being the string (an NSString), and the other the type (string or value).

joe
vikinara is offline   Reply With Quote
Old 03-03-2010, 02:04 PM   #6 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by vikinara View Post
Where am i going wrong? please help


myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];

NSArray *keys = [NSArray arrayWithObjects:@"Date","Time","Address","City"," State","Zipcode",nil];

[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:@"%@",txtDate.text],

[NSString stringWithFormat:@"%@",txtTime.text],

[NSString stringWithFormat:@"%@",txtAddress.text],

[NSString stringWithFormat:@"%@",txtCity.text],

[NSString stringWithFormat:@"%@",txtState.text],

[NSString stringWithFormat:@"%@",txtZip.text],nil]forKeys:keys]];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myPrimaryinfo.pli st"];
[myPrimaryinfo writeToFileath atomically:YES];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
You don't say what problem you're actually having, but this line is wrong:

Code:
NSArray *keys = [NSArray arrayWithObjects:@"Date","Time","Address","City"," State","Zipcode",nil];
All of the strings need to be objects, not just the first one. It needs to be:

Code:
NSArray *keys = [NSArray arrayWithObjects:@"Date",@"Time",@"Address",@"City",@" State",@"Zipcode",nil];
joe
FlyingDiver is offline   Reply With Quote
Old 03-03-2010, 04:41 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
Default need to save in the .plist

Hi

Thank u for the reply. I want to save the data in .plist. But i couldn't.Please help.

Quote:
Originally Posted by FlyingDiver View Post
You don't say what problem you're actually having, but this line is wrong:

Code:
NSArray *keys = [NSArray arrayWithObjects:@"Date","Time","Address","City"," State","Zipcode",nil];
All of the strings need to be objects, not just the first one. It needs to be:

Code:
NSArray *keys = [NSArray arrayWithObjects:@"Date",@"Time",@"Address",@"City",@" State",@"Zipcode",nil];
joe
vikinara is offline   Reply With Quote
Old 03-03-2010, 04:47 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
Default cont..

Also the .plist file s not created, i want to know if i am entering the string data in correct format. please advice.

thanks in advance
viki

Quote:
Originally Posted by vikinara View Post
Hi

Thank u for the reply. I want to save the data in .plist. But i couldn't.Please help.
vikinara is offline   Reply With Quote
Old 03-04-2010, 09:58 AM   #9 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
Default saving user input textfield value to be saved in .plist

Thank u so much. once i modified the strings to objects, it worked fine and i got the .plist file created also.

here is the code for saving user input textfield value to be saved in .plist

-(IBAction)textFieldCheckid) sender {



myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];

NSArray *keys = [NSArray arrayWithObjects:@"Date",@"Time",@"Address",@"City ",@"State",@"Zipcode",nil];

[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:@"%@",txtDate.text],

[NSString stringWithFormat:@"%@",txtTime.text],

[NSString stringWithFormat:@"%@",txtAddress.text],

[NSString stringWithFormat:@"%@",txtCity.text],

[NSString stringWithFormat:@"%@",txtState.text],

[NSString stringWithFormat:@"%@",txtZip.text],nil]forKeys:keys]];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myPrimaryinfo.pli st"];
[myPrimaryinfo writeToFileath atomically:NO];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);




Quote:
Originally Posted by vikinara View Post
Hi

Thank u for the reply. I want to save the data in .plist. But i couldn't.Please help.
vikinara is offline   Reply With Quote
Old 07-12-2010, 03:22 AM   #10 (permalink)
iPhoneDev
 
Join Date: Dec 2009
Posts: 33
Send a message via Yahoo to sivasankarp
Default Hi vikinara

I hope u resolved ur problem,can u send me ur sample code so tat i wil use it in my application.my plist was in this format as follows.kindly help me out

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>http://localhost:8888/sample</string>
<string>http://localhost:8888/sample</string>
</array>
</plist>

i wil send the url string from the implementation code.kindly help me in this pls..

-(IBAction)AddFieldid)sender;
{
NSMutableArray *myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];

NSArray *keys = [NSArray arrayWithObjects:@"string",nil];

[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:@"sample"],nil]forKeys:keys]];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"urls.plist"];
[myPrimaryinfo writeToFileath atomically:NO];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
}

Thanks



Quote:
Originally Posted by vikinara View Post
Thank u so much. once i modified the strings to objects, it worked fine and i got the .plist file created also.

here is the code for saving user input textfield value to be saved in .plist

-(IBAction)textFieldCheckid) sender {



myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];

NSArray *keys = [NSArray arrayWithObjects:@"Date",@"Time",@"Address",@"City ",@"State",@"Zipcode",nil];

[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:@"%@",txtDate.text],

[NSString stringWithFormat:@"%@",txtTime.text],

[NSString stringWithFormat:@"%@",txtAddress.text],

[NSString stringWithFormat:@"%@",txtCity.text],

[NSString stringWithFormat:@"%@",txtState.text],

[NSString stringWithFormat:@"%@",txtZip.text],nil]forKeys:keys]];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myPrimaryinfo.pli st"];
[myPrimaryinfo writeToFileath atomically:NO];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
sivasankarp is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray, plist, writetofile

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 263
21 members and 242 guests
ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, glenn_sayers, HemiMG, jakerocheleau, JasonR, LEARN2MAKE, nobre84, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:38 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0