Breaking News
Loading...
Monday, February 25, 2013

Work with Plist in iOS project

8:03 PM
step !: copy Plist file ,main bundle into document folder 

 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectoryNSUserDomainMaskYES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"DocList.plist"];
 
    if (![[NSFileManager defaultManagerfileExistsAtPath:plistPath])
    {
        NSString *bundle = [[NSBundle mainBundlepathForResource:@"Doclist" ofType:@"plist"];
        [[NSFileManager defaultManagercopyItemAtPath:bundle toPath:plistPath error:nil];

    }


2:Save Value To pList


 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectoryNSUserDomainMaskYES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"DocList.plist"];
    
    NSMutableArray *savedStock = [NSMutableArray  arrayWithContentsOfFile:plistPath];
    if(savedStock == nil)
    {
        savedStock = [[NSMutableArray alloc]initWithCapacity:0];
        
    }
    NSMutableDictionary *data = [[NSMutableDictionary allocinitWithCapacity:0];
    [data setObject:fileName forKey:@"docname"];
    [data setObject:@"Enter the keyword" forKey:@"keyword"];
    [data setObject:@"Email Address" forKey:@"f1"];
    [data setObject:@"Chat Screen Name" forKey:@"f2"];
    [savedStock addObject:data];
    
    [savedStock writeToFile:plistPath atomically:YES];
    [data release];



3:Get all Value from plist


 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectoryNSUserDomainMaskYES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"DocList.plist"];
    NSMutableArray *savedStock = [[NSMutableArray allocinitWithContentsOfFile: plistPath];
    
    for (int i=0 ; i <[savedStock count]; i++)
    {
         NSLog(@"value  %@",[[savedStock objectAtIndex:i] valueForKey:@"docname"]);
         NSLog(@"value  %@",[[savedStock objectAtIndex:i] valueForKey:@"keyword"]);
    }



4:Structure is like:



5:enjoy coding

0 comments:

 
Toggle Footer