0%

EFUtils示例(2)-转换

转换 Conversions

+stringFromDictionary:withKey:

字典中指定键值转换为字符串。(String value from the key value in a dictionary.)

1
2
3
4
5
6
7
8
9
10
11
NSDictionary *dictionary = @{@"ID": @59,
@"name": @"James",
@"age": [NSNull null],
@"offline": @true};
LOG_FORMAT(@"1: %@", [EFUtils stringFromDictionary:dictionary withKey:@"ID"]);
LOG_FORMAT(@"2: %@", [EFUtils stringFromDictionary:dictionary withKey:@"name"]);
LOG_FORMAT(@"3: %@", [EFUtils stringFromDictionary:dictionary withKey:@"age"]);

if (![EFUtils stringFromDictionary:dictionary withKey:@"age"]) { // age值不存在
// do some thing
}

+boolValueFromNumber:

将NSNumber转换为BOOL。(Bool value from a NSNumber object.)

1
2
3
4
5
6
7
8
9
NSDictionary *dictionary = @{@"ID": @59,
@"name": @"James",
@"age": [NSNull null],
@"offline": @true};
LOG_FORMAT(@"%d", [EFUtils boolValueFromNumber:dictionary[@"offline"]]);

if ([EFUtils boolValueFromNumber:dictionary[@"offline"]]) { // offline
// do some thing
}

+JSONToString:

JSON(字典或数组)转 JSON字符串。(JSON to string.)

1
2
3
4
5
NSDictionary *dictionary = @{@"ID": @59,
@"name": @"James",
@"age": @21,
@"offline": @true};
LOG_FORMAT(@"%@", [EFUtils JSONToString:dictionary]);

+stringToJSON:

JSON字符串转 JSON(字典或数组)。(String to JSON.)

1
2
NSString *string = @"{\"ID\":59, \"name\":\"James\", \"age\":21, \"offline\":1}";
LOG_FORMAT(@"%@", [EFUtils stringToJSON:string]);

下一期带来更多转换的示例。

相关

更新内容

相关链接调整。