OC-归档:json/plist
//归档: 就是把数据按照定义的格式存储到文件中,方便从文件恢复对象数据
//解归档: 从归档文件中读取数据到对象变量中
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//归档
/*NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@"helloworld"];
BOOL ret = [data writeToFile:@"/Users/zhangxueming/Desktop/zhang.plist" atomically:YES];
if (ret) {
NSLog(@"文件归档成功");
}
else
{
NSLog(@"文件归档失败");
}*/
#if 0
NSArray *arr= @[@"qian",@"feng",@"hello",@"world"];
BOOL ret1 = [NSKeyedArchiver archiveRootObject:arr toFile:@"/Users/zhangxueming/Desktop/abc.plist"];
if (ret1) {
NSLog(@"文件归档成功");
}
else
{
NSLog(@"文件归档失败");
}
#else
//解归档
//[NSKeyedUnarchiver unarchiveObjectWithData:]
// NSKeyedUnarchiver *obj=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/zhangxueming/Desktop/abc.plist"];
// NSLog(@"%@", obj);
NSData *data1 = [NSData dataWithContentsOfFile:@"/Users/zhangxueming/Desktop/abc.plist"];
NSKeyedUnarchiver *obj1 = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
NSLog(@"%@", obj1);
#endif
}
return 0;
}
//从本地json文件中解析数据
#import <Foundation/Foundation.h>
//json解析
//json语法: @[@"One", @"Two"] @{@"key":@"value",@"key2":@"value2"}
//类似于OC中的字典与数组嵌套
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *fileStr = [[NSString alloc] initWithContentsOfFile:@"/Users/zhangxueming/Desktop/json.txt" encoding:NSUTF8StringEncoding error:nil];
NSData *data = [fileStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"dict = %@",dict);
for (id obj in [dict valueForKey:@"weatherinfo"]) {
NSLog(@"%@", [[dict valueForKey:@"weatherinfo"] valueForKey:obj]);
}
}
return 0;
}
//从网络获取json文件解析
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//NSURL *url = [NSURL URLWithString: @"http://m.weather.com.cn/data/101010100.html"];
NSURL *url = [NSURL URLWithString: @"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.4.255.255"];
NSString *fileStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData *data = [fileStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dict = %@", dict);
}
return 0;
}
//通过右键创建plist文件
#import <Foundation/Foundation.h>
//保存用户信息, 程序执行信息, 配置参数
//如何创建一个plist文件
//plist 指针存储:NSArray NSDictonary NSData NSDate NSNumber Boolean NSString 对象
int main(int argc, const char * argv[])
{
@autoreleasepool {
#if 0
// 写文件
NSArray *arr= @[@"One",@"Two",@"three"];
NSDate *date= [NSDate date];
NSString *str = [NSString stringWithFormat:@"%@%d",@"hello world",45678];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:arr forKey:@"ARR"];
[dict setObject:date forKey:@"DATE"];
[dict setObject:str forKey:@"STRING"];
[dict writeToFile:@"/Users/zhangxueming/Desktop/ios1412/Day7_归档/OC3_plist/app.lrc" atomically:YES];//创建一个plist文件
#else
//读文件
//
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:@"/Users/zhangxueming/Desktop/ios1412/Day7_归档/OC3_plist/app.lrc"];
//[NSDictionary dictionaryWithContentsOfFile:@""];
//读取plist文件
//NSLog(@"dic1 = %@", dict1);
NSLog(@"str = %@", [dict1 objectForKey:@"STRING"]);
#endif
}
return 0;
}
通过代码创建plist文件
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
#if 0
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:@"qianfeng"];
[array addObject:@"helloworld"];
[array addObject:[NSDate date]];
[array addObject:[NSNumber numberWithDouble:3.14]];
[array addObject:[NSData dataWithContentsOfFile:@"/Users/zhangxueming/Desktop/json接口.rtf"]];
BOOL ret = [array writeToFile:@"/Users/zhangxueming/Desktop/stu.plist" atomically:YES];
if (ret) {
NSLog(@"文件创建成功");
}
else
{
NSLog(@"文件创建失败");
}
#else
//plist文件读取
NSArray * arr2 = [NSArray arrayWithContentsOfFile:@"/Users/zhangxueming/Desktop/stu.plist"];
NSLog(@"arr2 = %@", arr2);
#endif
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Laval : NSObject <NSCoding>
{
int _age;
NSString *_name;
}
@property (nonatomic)int age;
@property (nonatomic,copy)NSString *name;
@end
#import "Laval.h"
@implementation Laval
//编码
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeObject:self.name forKey:@"name"];
}
//解码
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
//[super initWithCoder:aDecoder]
//如果父类也遵守NSCoding协议, 先调用父类构造方法
self.age =[aDecoder decodeIntForKey:@"age"];
self.name = [aDecoder decodeObjectForKey:@"name"];
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name : %@, age = %i", self.name, self.age];
}
@end
#import <Foundation/Foundation.h>
#import "Laval.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
#if 0
Laval *xiaoQiang =[[Laval alloc] init];
xiaoQiang.age = 12;
xiaoQiang.name= @"小强";
BOOL ret = [NSKeyedArchiver archiveRootObject:xiaoQiang toFile:@"/Users/zhangxueming/Desktop/zhang.plist"];
if (ret) {
NSLog(@"归档成功");
}
else
{
NSLog(@"归档失败");
}
#else
//解归档
Laval *xiaoQiang = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/zhangxueming/Desktop/zhang.plist"];
//NSLog(@"name : %@ age : %i", xiaoQiang.name, xiaoQiang.age);
NSLog(@"%@", xiaoQiang);
#endif
}
return 0;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。