ios nsarry怎么转nsdata类型

 我来答
可乐爱吸管eh
2016-06-19 · TA获得超过215个赞
知道小有建树答主
回答量:387
采纳率:0%
帮助的人:202万
展开全部
// NSData NSDate NSString NSArray NSDictionary json

NSString *string = @"hello word";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSString *dateString = @"19891123";
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"hello", @"word",
@"chen", @"lu",
@"jin", @"mei",
@"cerastes", @"hehe",nil];

// NSString to NSData
NSData *string2data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",string2data);
// NSData to NSString
NSString *data2String = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"dataString = %@",data2String);

// NSArray to NSData

NSData *arry2data = [NSKeyedArchiver archivedDataWithRootObject:array];
// NSData to NSArray
NSArray *data2arry = [NSKeyedUnarchiver unarchiveObjectWithData:arry2data];
NSLog(@"%@ /n %@",arry2data,data2arry);

// NSString to NSDate
NSDate *string2date = [self convertDateFromString:dateString withFormattre:@"yyyyMMdd"];
NSLog(@"date = %@",string2date);

// NSDate to NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *date2string = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", date2string);

// NSArray to NSString

NSString *arry2String = [array componentsJoinedByString:@" "];

NSLog(@"string = %@",arry2String);

// NSString to NSArray
NSArray *string2Arry = [arry2String componentsSeparatedByString:@" "];
NSLog(@" %@",string2Arry);

// NSDictionary to NSData:
NSMutableData *dic2data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dic2data];
[archiver encodeObject:dic forKey:@"Some Key Value"];
[archiver finishEncoding];
NSLog(@"%@",dic2data);

// NSData to NSDictionary
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dic2data];
NSDictionary *data2dic = [unarchiver decodeObjectForKey:@"Some Key Value"];
[unarchiver finishDecoding];
NSLog(@"%@",data2dic);

// NSDictionary to NSString
NSString *dic2string=[dic JSONRepresentation]; //这个方法是SBJson类库里的,需要导入SBJson.h
NSLog(@"%@",dic2string);

// NSString to NSDictionary
NSError *error = nil;
NSDictionary *string2dic = [NSJSONSerialization JSONObjectWithData: [dic2string dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &error];
NSLog(@"%@",string2dic);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友cc948d4898
2016-06-19 · TA获得超过2286个赞
知道小有建树答主
回答量:255
采纳率:0%
帮助的人:411万
展开全部
软件找人写
啊堂
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式