簡體   English   中英

.Net與Cocoa字符串格式

[英].Net vs Cocoa String Formats

我可以復制以下C#/。NET:

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

在Objective-C /可可中:

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

但是我不能這樣做:

//C
String.Format("I have {0} dogs in my house.  My {0} dogs are very nice, but it is hard to walk {0} dogs at the same time.", numDogs);

在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" forKey@"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];

但我會滿足:

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

謝謝,

[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];

(請參見man 3 printf 。)

如果您使用的是NSStringstringWithFormat:那么我認為C99遵從性並不真正適用:而stringWithFormat:printf相似,但它已有一段時間了。

實際上, 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

(盡管我更喜歡將窗口標題欄中的代理圖標拖到我保存在Dock中的AppleScript小滴上: GenerateStrings.app 。它(過度)將生成的.strings文件寫入〜/ Desktop / Strings /(必要時創建它) )。您可以通過將腳本放到AppleScript編輯器中來對其進行編輯。

它將生成一個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 %2$@ %3$@ in my home.";

將其復制到西班牙語項目子文件夾,並根據需要更改項的順序(因為西班牙語中的形容詞通常在名詞之后):

Spanish.lproj / Localizable.strings

/* 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 %3$@ %2$@ 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! 如果有人在尋找類似的東西,這是我的解決方案。

// 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.0678m! 吉米快點!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM