【发布时间】:2011-01-20 16:18:23
【问题描述】:
我可以重现以下 C#/.NET:
//A
String.Format("There are {0} cats in my {1} and no {2}", 2, "house", "dogs");
在 Objective-C/Cocoa 中:
//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, ...);
谢谢,
【问题讨论】:
标签: c# .net objective-c cocoa string-formatting