2011-01-20 10 views
1

私は次のC#/を再現することができますNET:のObjective-C/Cocoaの中はココア文字列フォーマット対純

//A 
String.Format("There are {0} cats in my {1} and no {2}", 2, "house", "dogs"); 

//B 
[NSString stringWithFormat:@"There are %d cats in my %@ and no %@", 2, "house", "dogs"]; 

しかし、私はこれを行うことはできません。

Objective-Cの中 ​​

//D 
[NSString stringWithFormat:@"I have %d dogs in my house. My %d dogs are very nice, but it is hard to walk %d dogs at the same time.", numDogs]; 

はprintf形式などを使用しているためです。これを行うより高度な方法がありますか?文字列解析でKVCを実行する方法はありますか?

これは私が欲しいものを技術的には次のとおりです。

[player setValue:@"Jimmy" [email protected]"PlayerName"]; 
//later 
[NSString stringWithMagicFormat:@"<PlayerName> sat on a bench in the middle of winter, and <GenderPronoun> felt very cold." andPlayer:player]; 
// or 
[NSString stringwithMagicFormat: playerEnteredStringWithTagInIt andPlayer:player]; 

をしかし、私はのために解決されます:(。man 3 printfを参照)のおかげで、

答えて

5
[NSString stringWithFormat:@"I have %1$d dogs in my house. My %1$d dogs are very nice, but it is hard to walk %1$d dogs at the same time.", numDogs]; 

String.Format(playerEnteredStringWithTagInIt, player.PlayerName, player.PlayerGender, player.GenderPronoun, ...); 

+0

+1 internets DUH。 KVCについて何も投稿していない場合、私はこれを受け入れます。 –

+0

@Wevah、これはC99に準拠していませんか? ["C99標準には、Single Unix仕様に由来する '$'を使用したスタイルは含まれていません。]](http://linux.die.net/man/3/printf) –

+0

なぜそれが必要なのですか?あなたは 'gnu99'を使用できませんか? OS X用にコンパイルしている場合(これは "ココア"とタグ付けしていると仮定します)、OS Xのマニュアルページにはそのような警告はありません。 – Wevah

1

あなたが利用している場合g NSStringとの場合は、stringWithFormat:printfと似ていますが、しばらくの間は独自の実装がありますが、C99のコンプライアンスは実際には適用されません。

NSLocalizedString()マクロは自動的にこれを自動的に行います。例えば、私は次のコードを持っていると言う:

@implementation MDAppController 

- (id)init { 
    if (self = [super init]) { 
     NSArray *catsArray = [NSArray array]; 
     NSString *string = [NSString stringWithFormat: 
     NSLocalizedString(@"There are %lu %@ %@ in my home.", @"no comment"), 
     (unsigned long)[catsArray count], 
          NSLocalizedString(@"red", @""), 
          NSLocalizedString(@"cats", @"")]; 
     NSLog(@"string == %@", string); 
    } 
    return self; 
} 

@end 

私は、その後、ターミナル

で次のように実行した場合

/usr/bin/genstrings ~/Developer/NSLocalizedString/MDAppController.m -o ~/Desktop/Strings

(私はちょうどこの上にウィンドウのタイトルバーのプロキシアイコンをドラッグ好むけれどもAppleScriptのドロップレット私は自分のDock:GenerateStrings.appに保存します。生成された.stringsファイルを〜/ Desktop/Strings /に書き込みます(必要に応じて作成します)。

プロジェクトに追加できるUTF16 Localizable.stringsファイルが生成されます。スペイン語の形容詞で、通常は来るので、スペイン語のプロジェクトのサブフォルダにそれを複製し、(必要に応じて項目の順序を変更する

English.lproj /のLocalizable.strings

/* No comment provided by engineer. */ 
"cats" = "cats"; 

/* No comment provided by engineer. */ 
"red" = "red"; 

/* no comment */ 
"There are %lu %@ %@ in my home." = "There are %1$lu %[email protected] %[email protected] in my home."; 

:それは、それは次のようになります名詞の後に):

スペイン語.lproj /ローカライズ可能。文字列

/* No comment provided by engineer. */ 
"cats" = "gatos"; 

/* No comment provided by engineer. */ 
"red" = "rojos"; 

/* no comment */ 
"There are %lu %@ %@ in my home." = "Está %1$lu %[email protected] %[email protected] en mi casa."; 

英語は私の第一言語&テキスト県のペインで(一番上)の言語である:

:スペインは私の主な言語&テキスト県のペインで(一番上)の言語である場合には

1/20/2011 12:59:37 PM NSLocalizedString[1777] string == There are 0 red cats in my home.

1/20/2011 12:37:02 PM NSLocalizedString[1702] string == Está 0 gatos rojos en mi casa.

.stringsファイルはある意味でキー値のペア。詳細情報については

は、私はすべての人の答えを好むが、私はちょうどドキュメントで15分を費やしたと私はRTM、ため息...探していた答えを見つけることがResource Programming Guide: String Resources

0

を参照してください!他の誰かが同様のものを探している場合のために私の解決策はここにあります。

// Put this in a category of NSString 
- (NSString*) stringByReplacingOccurrencesOfKeysWithValues:(NSDictionary*)keyValuePairs 
{ 
    NSString *fStr = self; 
    if (keyValuePairs) { 
     for (NSString *key in [keyValuePairs allKeys]) { 
      NSString *value = [NSString stringWithFormat:@"%@",[keyValuePairs valueForKey:key]]; 
      fStr = [fStr stringByReplacingOccurrencesOfString:key withString:value]; 
     } 
    } 
    return fStr; 
} 

これは私のテストコードだった、正常に動作するようです:

// testing NSString+Keys 
NSMutableDictionary *mud = [NSMutableDictionary dictionaryWithCapacity:1]; 
[mud setValue:@"Jimmy" forKey:@"{PlayerName}"]; 
[mud setValue:[NSNumber numberWithInt:97] forKey:@"{HitPoints}"]; 
[mud setValue:[NSNumber numberWithFloat:1.0678f] forKey:@"{meters}"]; 
NSString *mystr = [@"Help me {PlayerName} before it is too late! I only have {HitPoints}hp left! And I am {meters}m away! {PlayerName} hurry!" stringByReplacingOccurrencesOfKeysWithValues:mud]; 
NSLog(mystr); 

出力された:

手遅れになる前に、私のジミーを助けます!私は97hpしか残っていない!そして、私は1.0678メートル離れています!ジミーは急いで!

関連する問題