IOS开发3---文件的读取 与 写入,修改

本文介绍了如何使用Objective-C语言操作XML文件,包括修改和读取数据的方法。

1.建立一个 NSObject ,我们取名为operatePlist

2.在operatePlist.h中:

#import <Foundation/Foundation.h>

@interface operatePlist : NSObject

+(void)modifyed:(NSString*)value forkey:(NSString*)key;

+(NSString *) readShop:(NSString *)shopTemp;

@end

3.operatePlist.m中

#import "operatePlist.h"

@implementation operatePlist

+(void)modifyed:(NSString*)value forkey:(NSString*)key{     //修改数据方法 

    NSFileManager *fileManager = [NSFileManager defaultManager];   //建立文件管理器

    //get the plist file from bundle

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); //获取
Library路径

    NSString *documentsDirectory = [NSString stringWithFormat:@"%@/Caches/",[paths objectAtIndex:0]];//获取Library/Caches目录

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"CustomIfro.plist"];//获取事先建的plist文件路径

    // build the array from the plist

    if ([fileManager fileExistsAtPath: plistPath]) 

    {

        NSMutableDictionary *  dict = [[NSMutableDictionary alloc]initWithContentsOfFile : plistPath];

        [[dict objectForKey:@"Student"] setValue:value forKey:key];

        //[dict setObject:@“” forKey:@“key”];  加入键值为key,值为@“”的物件

        [dict writeToFile : plistPath  atomically : YES  ] ;

        [dict release];

    }

    else {

        NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle]

                                                   pathForResource:@"CustomIfro"

                                                   ofType:@"plist"];

        NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];

        [[NSFileManager defaultManager] createFileAtPath:plistPath contents:mainBundleFile attributes:nil];

        NSMutableDictionary *  dict = [[NSMutableDictionary alloc]initWithContentsOfFile : plistPath];

        [[dict objectForKey:@"Student"] setValue:value forKey:key];

        //[dict setObject:@“” forKey:@“key”];  加入键值为key,值为@“”的物件

        [dict writeToFile : plistPath  atomically : YES  ] ;

        [dict release];

    }

}

+ (NSString *) readShop:(NSString *)shopTemp  //读数据方法

{

    NSString *temp = [[[NSString alloc]init]autorelease];

    //get the plist file from bundle

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [NSString stringWithFormat:@"%@/Caches/",[paths objectAtIndex:0]];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"CustomIfro.plist"];

    // build the array from the plist

    NSMutableDictionary *  dict = [[[NSMutableDictionary alloc]initWithContentsOfFile : plistPath]autorelease];

    temp = [[dict objectForKey:@"Student"] objectForKey:shopTemp];

    return temp;

}

@end

4.方法调用:

在调用的M文件中 引入#import "operatePlist.h"

[operatePlist modifyed:self.name.text forkey:@"Name"];  


 name.text=[operatePlist readShop:@"Name"];


转载于:https://my.oschina.net/u/661032/blog/75174

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值