Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 2, 2011, 09:06 AM   #1
Nnavick
macrumors regular
 
Join Date: Oct 2010
NSMutableArray into plist file

Hi,
I'm trying to add NSMutableArray into plist file,I wrote the next read and write codes:

write code:

Code:
  NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    //[data1 setObject:[NSNumber numberWithInt:value] forKey:@"value"];
    [Favorites addObject:CheckInButton];
    //[data1 setObject:Favorites forKey:@"niv"];
    [data1 setObject:[NSMutableArray arrayWithArray:Favorites] forKey:@"niv"];
    [data1 writeToFile: path atomically:YES];
   [data1 release];
read code:

Code:
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    //value1 = [[savedStock objectForKey:@"value"] intValue];
    NSMutableArray *Arr=[[NSMutableArray alloc]init];
    Arr=[savedStock objectForKey:@"niv"];
    NSLog(@"plist is %d",[Arr count]);
    [savedStock release];
Favorites is an mutablearray:
Code:
 Favorites=[[NSMutableArray alloc]init];
when I'm trying to pull the NSMutableDictionary the count is 0,
why it's happening ?

Thanks a lot!
Nnavick is offline   0 Reply With Quote
Old May 2, 2011, 09:27 AM   #2
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by Nnavick View Post
Hi,
I'm trying to add NSMutableArray into plist file,I wrote the next read and write codes:

write code:

Code:
  NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    //[data1 setObject:[NSNumber numberWithInt:value] forKey:@"value"];
    [Favorites addObject:CheckInButton];
    //[data1 setObject:Favorites forKey:@"niv"];
    [data1 setObject:[NSMutableArray arrayWithArray:Favorites] forKey:@"niv"];
    [data1 writeToFile: path atomically:YES];
   [data1 release];
What is CheckInButton? Plists can only contain a few types, like NSString, NSNumber and NSData. You cannot put a UIButton into this array without first encoding it as NSData. You cannot put references to UI-type objects in your program into an array and expect them to resolve when you pull it out of the file.
Quote:
read code:

Code:
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    //value1 = [[savedStock objectForKey:@"value"] intValue];
    NSMutableArray *Arr=[[NSMutableArray alloc]init];
    Arr=[savedStock objectForKey:@"niv"];
    NSLog(@"plist is %d",[Arr count]);
    [savedStock release];
The code in red is a memory leak. You allocate a pointer for the array and then overwrite it. You need to get the array first and then retain it. Also, plist format does not recognize mutability: to make it a mutable array, you either have to use -mutableCopy or -initWithArray: (the latter would probably be better).

Try "NSLog( @"%@", [Arr description] );" to see what you are getting.
__________________
You got to be a spirit. You can't be no ghost.

Last edited by Sydde; May 2, 2011 at 09:48 AM.
Sydde is offline   0 Reply With Quote
Old May 2, 2011, 11:50 AM   #3
Nnavick
Thread Starter
macrumors regular
 
Join Date: Oct 2010
how do I change to button to nsdata?

if the NSMutableArray contain several button and I will copy the NSMutableArray and store it in the plist will I have the same problem?
Nnavick is offline   0 Reply With Quote
Old May 2, 2011, 01:08 PM   #4
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
What are you trying to accomplish? Storing buttons in an array does not really make sense. Can you store information that you need to reconstitute the button and then make a UIButton subclass that could build itself from that info?

To create NSData from some object involves encoding it, reconstitution involves decoding. This is useful stuff to know, you should read the documentation titled "Archives and Serialization" (linked from the NSKeyedArchiver class reference). Again, a button encoded as NSData will not be able to decode any references that are external to it. You cannot encode the button's target with the button and expect it to decode to what you want. You are much better of just writing a method that can read some information from the array and use that to rebuild a button.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old May 2, 2011, 02:01 PM   #5
Nnavick
Thread Starter
macrumors regular
 
Join Date: Oct 2010
Quote:
Originally Posted by Sydde View Post
What are you trying to accomplish? Storing buttons in an array does not really make sense. Can you store information that you need to reconstitute the button and then make a UIButton subclass that could build itself from that info?

To create NSData from some object involves encoding it, reconstitution involves decoding. This is useful stuff to know, you should read the documentation titled "Archives and Serialization" (linked from the NSKeyedArchiver class reference). Again, a button encoded as NSData will not be able to decode any references that are external to it. You cannot encode the button's target with the button and expect it to decode to what you want. You are much better of just writing a method that can read some information from the array and use that to rebuild a button.
Good idea,but how do I store all the button information in one place ?
Nnavick is offline   0 Reply With Quote
Old May 2, 2011, 02:43 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Use an NSDictionary.

Step 1: Using pencil and paper, write down a list of all the important button characteristics you need to save. For example, its title, its size and/or position, etc. Leave out any characteristics you don't need to save, or which are the same for every button.

Step 2a: Give each important characteristic in your list a unique name. For example, "title" for the title string, "size" for its size, etc. This unique name will be the key into an NSDictionary.

Step 2b: For each characteristic, decide which plist type will be used to represent the value. For example, strings are NSString, integers and floats are NSNumber. Values that are composed of other types, such as coordinates, should be broken down into their sub-components, such as two or more numbers. Make sure each value is represented as a valid plist type: NSString, NSNumber, NSArray, etc. Refer to the plist documentation to ensure you know what the valid types are.

Step 3: Write code that gets each characteristic value and stores it as the properly typed value associated with the proper unique key from Step 2.

Step 4: Add the NSDictionary at the proper place in the plist structure.

Steps 1 & 2 are design steps. Steps 3 and 4 are coding steps. You have to design a solution before you code it.

Step 5: To reverse the process, design code that takes an NSDictionary and using the unique keys from Step 2, sets properties and other characteristics of a UIButton. Essentially, your code builds a UIButton from information given to it in an NSDictionary.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: Carrier.plist file Jumpie Jailbreaks and iOS Hacks 1 Dec 26, 2013 09:37 PM
Anyone have their info.plist file? SCOLANATOR OS X Mavericks (10.9) 0 Nov 25, 2013 04:37 PM
Am I doing this wrong? Looking for a plist file... xheathen Mac Applications and Mac App Store 5 Sep 23, 2013 01:39 PM
iPhone 4S Panic.plist file idontjailbreak iPhone Tips, Help and Troubleshooting 0 Mar 28, 2013 02:20 PM
Resolved: Saving to Plist file. Mvkoe iPhone/iPad Programming 22 Jan 7, 2013 11:47 AM

Forum Jump

All times are GMT -5. The time now is 03:09 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC