您的位置:首页 > 其它

20150623_OC之属性列表文件plist

2015-07-03 21:04 369 查看
plist文件的结构及操作:
//
//  ParsePlist.h
//  IOS150623_ObjectiveC_Plist文件1
//
//  Created by PengJunlong on 15/6/23.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ParsePlist : NSObject
- (void)parseFile:(NSDictionary *)dic;
@end


//
//  ParsePlist.m
//  IOS150623_ObjectiveC_Plist文件1
//
//  Created by PengJunlong on 15/6/23.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "ParsePlist.h"

@implementation ParsePlist

- (void)parseFile:(NSDictionary *)dic
{
NSDictionary *dic1 = [dic objectForKey:@"frames"];
char keyName[20];
char propertyName[20];
printf("输入:");
scanf("%s%s",keyName,propertyName);
NSDictionary *subOfDic1 = [dic1 objectForKey:[NSString stringWithUTF8String:keyName]];
if (subOfDic1) {
NSString *valueStr = [subOfDic1 objectForKey:[NSString stringWithUTF8String:propertyName]];
NSLog(@"subOFDic1 = %@",valueStr);
}

}
@end


//
//  main.m
//  IOS150623_ObjectiveC_Plist文件1
//
//  Created by PengJunlong on 15/6/23.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ParsePlist.h"

//***************************
//*         plist文件        *
//***************************

//plist(properties list)文件的只有一个根节点,根只能是数组(NSArray)或者是字典(NSDictionary),不能为其他的类型,plist文件里可以直接进行添加删除
//plist中存储的对象:NSArray, NSDcitionary, NSDate, NSData, NSNumber, NSString, Boolean

int main(int argc, const char * argv[]) {
@autoreleasepool {

NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",[NSNumber numberWithInt:23],[NSDate date], nil];
//在指定目录下没有对应的plist文件,会自动创建该文件
BOOL ret = [array writeToFile:@"/Users/qianfeng/Desktop/test/dic3/app.plist" atomically:YES];
if (ret) {
NSLog(@"写入成功");
}
else
{
NSLog(@"写入失败");
}

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",[NSNumber numberWithInt:22],@"num",[@"IOS" dataUsingEncoding:NSUTF8StringEncoding],@"ios",[NSDate date],@"date",array,@"array", nil];
BOOL ret1 = [dictionary writeToFile:@"/Users/qianfeng/Desktop/test/dic3/array.plist" atomically:YES];
if (ret1) {
NSLog(@"写入成功");
}
else
{
NSLog(@"写入失败");
}
//只能读取plist文件中根节点是Dictionary
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_ObjectiveC_Plist文件1/apple.plist"];
NSLog(@"dic = %@",dic);

//只能读取plist文件中根节点是Array
NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/qianfeng/Desktop/test/dic3/app.plist"];
NSLog(@"array = %@",arr);

//--------------解析plist文件-------------
NSDictionary *dict1 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150623_ObjectiveC_Plist文件1/qfile.plist"];
ParsePlist *parse = [[ParsePlist alloc] init];
[parse parseFile:dict1];
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: