Objective-C specifications for formatting
Objective-C format strings and C # are very different. Let's take a look at them.
InC #We can do this in a few simple examples:
1 // format the output string 2 string word = "world"; 3 string test = string. format ("hello {0}", word); 4 5 // Format the output number 6 int num = 100; 7 string myNum = string. format ("myNum = {0}", num); 8 9 // Format the output time 10 DateTime nowTime = DateTime. now; 11 string currentDate = string. format ("nowTime = {0}", nowTime );
InObjective-CWe need to perform the conversion in this way:
1 // format the string 2 NSString * world = @ "world"; 3 NSLog (@ "hello % @", world ); 4 5 // format an integer 6 int num = 2; 7 NSLog (@ "% d", num );
% @ |
Object |
% D, % I |
Integer |
% U, % z |
Unoperator integer |
% F |
Floating Point/double-Character |
% X, % X |
Hexadecimal integer |
% O |
Octal integer |
% Zu |
Size_t |
% P |
Pointer |
% E |
Floating Point/double-character (Scientific Computing) |
% G |
Floating Point/double-Character |
% S |
C string |
%. * S |
Pascal string |
% C |
Character |
% C |
Unichar |
% Lld |
64-bit long integer (long) |
% Llu |
64-bit long integer |
% Lf |
64-bit dual-Character |
% Hhd |
Boolean Type |