Peer Mohamed Thabib
Peer Mohamed Thabib

Reputation: 705

how to write array of elements in to plist

- (void)viewDidLoad
{
NSError *error;
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *path1 = [documentDirectory stringByAppendingPathComponent:@"data.plist"];
NSFileManager *filemanager = [NSFileManager defaultManager];
NSLog( @"%@" ,documentDirectory) ;
if(![filemanager fileExistsAtPath:path1])
{
    NSString *bundle = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    [filemanager copyItemAtPath:bundle toPath:path1 error:&error];
}


NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:documentDirectory] ;

NSMutableArray *arryRandomNumber =[[NSMutableArray alloc]init];
while (arryRandomNumber.count<10) {
    NSInteger randomNumber=1+arc4random()%10;
    if (![arryRandomNumber containsObject:[NSString stringWithFormat:@"%d",randomNumber]])       {
        [arryRandomNumber addObject:[NSString stringWithFormat:@"%d",randomNumber]];
        [dictMioDB setValue: arryRandomNumber forKey:?????????];
    }
    continue;
}

[dictMioDB writeToFile:path1 atomically:YES];
NSLog(@"%@ - %@",arryRandomNumber,[arryRandomNumber objectAtIndex:2]);

//[data writeToFile: path atomically:YES];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

i am new to objective c and i am trying to write array of elements in to my plist

Upvotes: 0

Views: 143

Answers (3)

Alex
Alex

Reputation: 403

Try like this

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"username.plist"];


    NSString *x = @"xxx";
    NSString *y = @"yyy";
    NSString *z = @"zzz";

    //Load the original file
    NSMutableArray *arr;
    if([[NSFileManager defaultManager] fileExistsAtPath:plistPath])   
        //File exist load
        arr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    else
        //File does not exist create
        arr = [[NSMutableArray alloc] init];

    //Create dictionary
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: x,y,z,nil] forKeys:[NSArray arrayWithObjects: @"object1",@"object2",@"object3", nil]];

    //Append to arr
    [arr removeAllObjects];
    [arr addObject:plistDict];

    //Save to file
    [arr writeToFile:plistPath atomically:YES];

Hope it will helps you...

Upvotes: 0

Wain
Wain

Reputation: 119031

This line:

NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:documentDirectory] ;

Is a problem because you're trying to load a full directory, not a plist file.

So, when you come to this line:

[dictMioDB setValue: arryRandomNumber forKey:?????????];

You're trying to add the values to a dictionary that doesn't exist. And then when you try to save it there is nothing to save.

Upvotes: 1

Divya Bhaloidiya
Divya Bhaloidiya

Reputation: 5064

If you use array then use :

[urArray writeToFile:pListFilePath atomically:YES];

If in order to use dictionary :

[urDictionary writeToFile:(NSString *)filePath atomically:YES]; 

Upvotes: 0

Related Questions