09-12-2009, 07:59 PM
#1 (permalink )
Registered Member
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
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) 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!
09-12-2009, 08:46 PM
#2 (permalink )
Former NeXTStep Developer
Join Date: Mar 2009
Posts: 997
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
09-12-2009, 08:52 PM
#3 (permalink )
Registered Member
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Then how would you recommend storing this data so that it can also be read?
09-12-2009, 08:57 PM
#4 (permalink )
Former NeXTStep Developer
Join Date: Mar 2009
Posts: 997
Quote:
Originally Posted by
freshking
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
03-03-2010, 02:01 PM
#5 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 84
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 writeToFile
ath atomically:YES];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
Quote:
Originally Posted by
FlyingDiver
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
03-03-2010, 02:04 PM
#6 (permalink )
Former NeXTStep Developer
Join Date: Mar 2009
Posts: 997
Quote:
Originally Posted by
vikinara
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 writeToFile
ath 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
03-03-2010, 04:41 PM
#7 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 84
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
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
03-03-2010, 04:47 PM
#8 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 84
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
Hi
Thank u for the reply. I want to save the data in .plist. But i couldn't.Please help.
03-04-2010, 09:58 AM
#9 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 84
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)textFieldCheck
id) 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 writeToFile
ath atomically:NO];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
Quote:
Originally Posted by
vikinara
Hi
Thank u for the reply. I want to save the data in .plist. But i couldn't.Please help.
07-12-2010, 03:22 AM
#10 (permalink )
iPhoneDev
Join Date: Dec 2009
Posts: 33
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)AddField
id)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 writeToFile
ath atomically:NO];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
}
Thanks
Quote:
Originally Posted by
vikinara
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)textFieldCheck
id) 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 writeToFile
ath atomically:NO];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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