0

您好,只是一个简单的问题,在 NSLocale 类中 – displayNameForKey:value: 和 – objectForKey: 有什么区别?我在网上搜索但没有得到它。谢谢你。

苹果文档

displayNameForKey:value:
Returns the display name for the given value.

- (NSString *)displayNameForKey:(id)key value:(id)value
Parameters
key
Specifies which of the locale property keys value is (see “Constants”),
value
A value for key.
Return Value
The display name for value.



objectForKey:
Returns the object corresponding to the specified key.

- (id)objectForKey:(id)key
Parameters
key
The key for which to return the corresponding value. For valid values of key, see “Constants.”
Return Value
The object corresponding to key.
4

1 回答 1

3

您使用 displayNameForKey:value: 根据您正在调用的语言环境对象获取适合显示的标签。例如:

NSLocale *french = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *frenchName = [french displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLocale *english = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
NSString *englishName = [english  displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLog(@"French locale in French is called '%@'", frenchName);
NSLog(@"French locale in English is called '%@'", englishName);

将产生输出:

法语中的法语语言环境称为“français (France)”
英语中的法语语言环境称为“French (France)”

NSLocale 类参考中有很多示例。

于 2012-06-03T22:16:36.640 回答