.Net vs Cocoa String Formats

Ste*_*ani 1 .net c# cocoa objective-c string-formatting

我可以重现以下C#/ .NET:

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

在Objective-C/Cocoa中:

//B
[NSString stringWithFormat:@"There are %d cats in my %@ and no %@", 2, "house", "dogs"];
Run Code Online (Sandbox Code Playgroud)

但我不能这样做:

//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);
Run Code Online (Sandbox Code Playgroud)

在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];
Run Code Online (Sandbox Code Playgroud)

因为它使用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];
Run Code Online (Sandbox Code Playgroud)

但我会满足于:

String.Format(playerEnteredStringWithTagInIt, player.PlayerName, player.PlayerGender, player.GenderPronoun, ...);
Run Code Online (Sandbox Code Playgroud)

谢谢,

Wev*_*vah 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];
Run Code Online (Sandbox Code Playgroud)

(见man 3 printf.)