1. 程式人生 > >iOS巔峰之NSString,NSData,NSArray 之間的轉換

iOS巔峰之NSString,NSData,NSArray 之間的轉換

1.將陣列轉換成NSData

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataArray options:NSJSONWritingPrettyPrinted error:nil];


2.將NSData轉換成NSString

NSString *fileString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

3.將字串轉換成NSData

NSData* aData= [str dataUsingEncoding: NSUTF8StringEncoding];

4.將NSData轉換成(NSString, NSArray)

id jsonObject = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingAllowFragments error:nil];

NSArray *array = (NSArray *)jsonObject; // 或者 NSString *string = (NSString *)jsonObject;