iOS常用的数据类型转换

1. String -> Data
2. Data -> String
3. Data -> Base64EncodedString
4. Base64EncodedString -> Data -> String
5. Dictory -> Data
6. Data -> Dictory
7. Json -> Dictory
8. Dictory -> Json

Swift

1. String -> Data
let string = "No Today"
guard let data = string.data(using: .utf8) else { return }
2. Data -> String
guard let str = String(data: data, encoding: .utf8) else { return }
3. Data -> Base64EncodedString
let base64String = data.base64EncodedString()
4. Base64EncodedString -> Data -> String
guard let base64Data = Data(base64Encoded: base64String) else { return }
guard let string = String(data: base64Data, encoding: .utf8) else { return }
5. Dictory -> Data
let dictory = ["name": "once"]
guard let data = try? JSONSerialization.data(withJSONObject: dictory, options: .prettyPrinted) else { return }
6. Data -> Dictory
guard let dictory = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else { return }
7. Json -> Dictory
let json = "{\"name\":\"once\"}"
guard let data = json.data(using: .utf8, allowLossyConversion: true) else { return }
guard let dictory = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else { return }
8. Dictory -> Json
let dictory = ["name": "once"]
guard let data = try? JSONSerialization.data(withJSONObject: dictory, options: .prettyPrinted) else { return }
guard let json = String(data: data, encoding: .utf8) else { return }

OC

1. NSDictionary -> JSON
NSDictionary *dic = @{
                      @"time"   : @"2018-04-03",
                      @"item"   : @"NSJSONSerialization",
                      @"data"   : @{
                                    @"info": @"12"
                                    }
                      };
NSString *filePath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
filePath = [filePath stringByAppendingPathComponent:@"dic"];
    
/*  NSJSONWritingOptions  枚举
 *  NSJSONWritingPrettyPrinted  将JSON数据格式化,可读性高
 *  NSJSONWritingSortedKeys     不做任何操作,返回一串字符串
 */ 
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
if (error){
    NSLog(@"object to json data error");
    return;
}else {
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}   
  
/* NSJSONWritingPrettyPrinted 输出
     {
       "data" : {
                  "info" : "12"
                },
       "time" : "2018-04-03",
       "item" : "NSJSONSerialization"
     }
 */
        
// NSJSONWritingSortedKeys  输出  {"data":{"info":"12"},"item":"NSJSONSerialization","time":"2018-04-03"}
2. JSON -> NSDictionary
/*  NSJSONReadingOptions 枚举
 *  NSJSONReadingMutableContainers 返回的是可变容器
 *  NSJSONReadingMutableLeaves     返回的内部的值可变对象
 *  NSJSONReadingAllowFragments    允许返回最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON Fragment.
 */
NSError *error;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (error) {
    NSLog(@"json data to object error");
    return;
}

你可能感兴趣的:(iOS常用的数据类型转换)