#import <Foundation/Foundation.h>
@interface CreatePlist : NSObject
@property(nonatomic,copy)NSString *plistName;//文件名
@property(nonatomic,copy)NSString *path;//文件路径
@property(nonatomic,strong)NSArray *dataArray;//数据
-(id)initWithName:(NSString *)name andPath:(NSString *)path andDataArray:(NSArray *)dataArray;
-(BOOL)createPlist; //生成plist文件
-(void)insertDataInPlist;//插入数据
+(void)insertDataInPlistWithFilePath:(NSString *)path andData:(id)data;//插入数据
@end
#import "CreatePlist.h"
@implementation CreatePlist
-(id)initWithName:(NSString *)name andPath:(NSString *)path andDataArray:(NSArray *)dataArray{
if (self = [super init]) {
_plistName = name;
_path = [NSString stringWithFormat:@"%@/%@.plist",path,name];
_dataArray = dataArray;
}
return self;
}
-(BOOL)createPlist{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *err = nil;
if ([fm fileExistsAtPath:_path]) {//判断文件夹是否存在
NSLog(@"wenjiancin");
return NO;
}else {
[fm createFileAtPath:_path contents:nil attributes:nil];//生成文件夹
if (err) {
return NO;//生成失败
}else{
return YES;//生成成功
}//else
}//else
return YES;
}
-(void)insertDataInPlist{ //将数据加入plist
[_dataArray writeToFile:_path atomically:YES];
}
+(void)insertDataInPlistWithFilePath:(NSString *)path andData:(id)data{//类方法 将数据加入plist文件
[data writeToFile:path atomically:YES];
}
@end