读写plist文件

写入plist文件

NSString *plistPath = @"/private/var/mobile/Media/iTunes_Control/iTunes/Ringtones.plist";

            NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

            [[dictionary objectForKey:@"Ringtones"] setObject:ringtones forKey:ringName];

            if (![dictionary writeToFile:plistPath atomically:YES]) {

                NSLog(@"The new ringtone write to file failured!");

            }

 

 

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Setup" ofType:@"plist"];    //把Setup.plist文件放到项目中就ok了,文件名随意。

    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; //根据plistPath内容,将Setup.plist内容读入到一个NSMutableDictionary中 //如果仅仅是读取<u><font color="\"red\"">plist</font></u>内容并显示的话,此处用NSDictionary即可。
     
    //将服务器信息填入视图
    serverIP    = [[dictionary objectForKey:@"ServerSetup"] objectForKey:@"Server"];  //读取"ServerSetup"层 的"Server"关键字下的内容
    serverPort  = [[dictionary objectForKey:@"ServerSetup"] objectForKey:@"Port"];
     
   //显示读取的内容。
    UIAlertView* alert =[[UIAlertView alloc] initWithTitle:@"IP地址" message:serverIP delegate:Nil cancelButtonTitle:@"OKey" otherButtonTitles:nil, nil];
    [alert show];
     
//将读取到的内容 放入变量中
    AccelemeterX = (int)[[dictionary objectForKey:@"Accelerometer"] objectForKey:@"X"];  
    AccelemeterY = (int)[[dictionary objectForKey:@"Accelerometer"] objectForKey:@"Y"];  
    AccelemeterZ = (int)[[dictionary objectForKey:@"Accelerometer"] objectForKey:@"Z"];  
    NSLog(@"x=%@,y=%@,z=%@",AccelemeterX,AccelemeterY,AccelemeterZ);
  
 
 
 
    //如下进行写<u><font color="\"red\"">plist</font></u>操作,注意写<u><font color="\"red\"">plist</font></u>操作,必须使用NSMutableDictionary才行
 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Setup" ofType:@"plist"];             //把Setup.plist文件放到项目中就ok了,文件名随意。
 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];   //根据plistPath内容,此处必须使用NSMutableDictionary
    [[dictionary objectForKey:@"ServerSetup"] setValue:@"abc.abc.abc.abc" forKey:@"Server"];          //将<u><font color="\"red\"">plist</font></u>文件中“ServerSetup”下的“Server”关键字的内容改为“abc.abc.abc.abc”  
    [dictionary writeToFile:plistPath atomically:YES];    //要想将修改内容写入文件,必须执行这步,否则即使修改了也是没有落实到磁盘上的。
 
http://www.cocoachina.com/bbs/read.php?tid=104013&keyword=plist
posted on 2012-06-08 16:47  easonoutlook  阅读(419)  评论(0编辑  收藏  举报