如何从 NSDictionary 获取 NSArray 名称?
How to fetch a NSArray name from NSDictionary?
这是我设置或创建 NSDictionary 对象的方式:
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:[NSArray arrayWithObjects:@"ar", nil] forKey:@"AppleLanguages"];
[userDefault synchronize];
而且,我在这里获取并存储到本地 NSDictionary:
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSDictionary *dictLanguage = [userDefault objectForKey:@"AppleLanguages"];
[userDefault synchronize];
这里我想用同样的方法检查是@"ar"还是@"en":
if ([[[dictLanguage objectForKey:@"AppleLanguages"] isEqualToString:@"ar"]]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_AR];
}
else if ([[dictLanguage objectForKey:@"AppleLanguages"] isEqualToString:@"en"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_EN];
}
如果您存储了一个 NSArray ,那么您必须将其读取为 NSArray 而不是 NSDictionary 也可以使用此代码来检查当前语言
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSArray *arrLang = [userDefault objectForKey:@"AppleLanguages"];
if ([arrLang[0] isEqualToString:@"ar"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_AR];
}
else if ([arrLang[0] isEqualToString:@"en"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_EN];
}
您的 userDefault 结构如下所示:
{
"AppleLanguages" : [
"ar"
]
}
所以,你的字典有 AppleLanguages as key 和 array of values as 值。当您访问此键时,您会得到一个数组而不是字典。
所以,而不是
NSDictionary *dictLanguage = [userDefault objectForKey:@"AppleLanguages"];
使用
NSArray *arrLanguage = [userDefault objectForKey:@"AppleLanguages"];
你的 If 条件应该是
if ([arrLanguage[0] isEqualToString:@"ar"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_AR];
}
else if ([arrLanguage[0] isEqualToString:@"en"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_EN];
}
这是我设置或创建 NSDictionary 对象的方式:
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:[NSArray arrayWithObjects:@"ar", nil] forKey:@"AppleLanguages"];
[userDefault synchronize];
而且,我在这里获取并存储到本地 NSDictionary:
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSDictionary *dictLanguage = [userDefault objectForKey:@"AppleLanguages"];
[userDefault synchronize];
这里我想用同样的方法检查是@"ar"还是@"en":
if ([[[dictLanguage objectForKey:@"AppleLanguages"] isEqualToString:@"ar"]]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_AR];
}
else if ([[dictLanguage objectForKey:@"AppleLanguages"] isEqualToString:@"en"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_EN];
}
如果您存储了一个 NSArray ,那么您必须将其读取为 NSArray 而不是 NSDictionary 也可以使用此代码来检查当前语言
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSArray *arrLang = [userDefault objectForKey:@"AppleLanguages"];
if ([arrLang[0] isEqualToString:@"ar"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_AR];
}
else if ([arrLang[0] isEqualToString:@"en"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_EN];
}
您的 userDefault 结构如下所示:
{
"AppleLanguages" : [
"ar"
]
}
所以,你的字典有 AppleLanguages as key 和 array of values as 值。当您访问此键时,您会得到一个数组而不是字典。
所以,而不是
NSDictionary *dictLanguage = [userDefault objectForKey:@"AppleLanguages"];
使用
NSArray *arrLanguage = [userDefault objectForKey:@"AppleLanguages"];
你的 If 条件应该是
if ([arrLanguage[0] isEqualToString:@"ar"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_AR];
}
else if ([arrLanguage[0] isEqualToString:@"en"]) {
strCategoryUrl = [NSString stringWithFormat:@"%@%@", ROOT_URL, API_PRODUCT_MAIN_EN];
}