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 Class Referenceには多くの例があります。

于 2012-06-03T22:16:36.640 に答える