NSMutableArray into plist file

Discussion in 'iOS Programming' started by Nnavick, May 2, 2011.

  1. Nnavick macrumors regular

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

    write 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:

        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:
     Favorites=[[NSMutableArray alloc]init];
    when I'm trying to pull the NSMutableDictionary the count is 0,
    why it's happening ?

    Thanks a lot!
  2. Sydde, May 2, 2011
    Last edited: May 2, 2011

    Sydde macrumors 68020


    Aug 17, 2009
    Velvet Green
    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.
    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.
  3. Nnavick thread starter macrumors regular

    Oct 13, 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?
  4. Sydde macrumors 68020


    Aug 17, 2009
    Velvet Green
    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.
  5. Nnavick thread starter macrumors regular

    Oct 13, 2010
    Good idea,but how do I store all the button information in one place ?
  6. chown33 macrumors 604

    Aug 9, 2009
    descending into the Maelström
    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.

Share This Page