[英]I want to check for a input values of a textfield is float or int or string in iOS
[英]I want to replace Json values to another string values in iOS
使用Json,我已經檢索了UITableViewCell的一些值
NSString *sample = [[array objectAtIndex:indexPath.row] objectForKey:@"food"];
我在示例字符串值中得到了1,2,3,4,6
但我想展示一下,例如1=apple 2=grape 3=orange 4=pineapple 5=lemon 6=All
我希望結果是apple,grape,orange,pineapple,All
在我的示例字符串值中
在Objective-C
,沒有內置的enums
可用於String
類型。 您可以將enums
聲明為Integer
,然后創建一個函數或字符串數組,這些函數或字符串數組返回該enum
的字符串值。
在.h
文件中聲明。
typedef NS_ENUM(NSInteger, FRUIT) {
APPLE = 1,
GRAPE,
ORANGE,
PINEAPPLE,
LEMON,
All
};
extern NSString * const FRUITString[];
在您的.m
文件中定義。
NSString * const FRUITString[] = {
[APPLE] = @"APPLE",
[GRAPE] = @"GRAPE",
[ORANGE] = @"ORANGE",
[PINEAPPLE] = @"PINEAPPLE",
[LEMON] = @"LEMON",
[All] = @"All"
};
您可以像這樣使用它:
NSLog(@"%@", FRUITString[APPLE]);
// OR
NSLog(@"%@", FRUITString[1]);
輸出: APPLE
根據您的評論: 如果食物價值是“食物”:“ 1,2”,我想顯示“蘋果,葡萄”
NSString *foodValue = @"1,2";
NSArray *values = [foodValue componentsSeparatedByString:@","];
NSMutableArray *stringValues = [[NSMutableArray alloc] init];
for (NSString *value in values) {
[stringValues addObject:FRUITString[[value integerValue]]];
}
NSLog(@"%@", [stringValues componentsJoinedByString:@","]);
蘋果,葡萄
使用所需的映射定義NSDictionary,然后從映射字典中獲取值:
NSDictionary *foodDict = @{
@"1": @"apple",
@"2": @"grape",
@"3": @"orange",
@"4": @"pineapple",
@"5": @"lemon",
@"6": @"all"
};
NSString *sample = [[array objectAtIndex:indexPath.row] objectForKey:@"food"];
NSString *sampleValue= [foodDict valueForKey:sample];
“ sampleValue”是所需的必需字符串值。 (如果需要,將其轉換為NSString)
將您的代碼objectForKey @“”更改為valueForKey @“”
NSString *sample = [[array objectAtIndex:indexPath.row] valueForKey:@"food"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.