Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
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!
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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:[B][COLOR="Blue"]CheckInButton[/COLOR][/B]];
    //[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.
read code:

Code:
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    //value1 = [[savedStock objectForKey:@"value"] intValue];
    [COLOR="Red"]NSMutableArray *Arr=[[NSMutableArray alloc]init];[/COLOR]
    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.
 
Last edited:

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
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?
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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.
 

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
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 ?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.