objective-c的%s和%@

%s和%@

%s是c标准的字符串

而%@是objective-C的字符串NSString

 

补充


%@ 对象 
%d 十进制整数 
%i 十进制整数 
%u 十进制无符号整型 
%o 八进制整数 
%x 十六进制整数 
%e 指数形式的浮点数 
%f 浮点数 
%s 字符串 
%c 字符 
%p 指针

格式转换

NSString *tempA = @"123";

NSString *tempB = @"456";

1,字符串拼接

 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];

2,字符转int

int intString = [newString intValue];

3,int转字符

NSString *stringInt = [NSString stringWithFormat:@"%d",intString];

4,字符转float

 float floatString = [newString floatValue];

5,float转字符

NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];

int和NSInteger

Objective-C里,苹果的官方文档中总是推荐用NSInteger

在苹果的api实现中,NSInteger是一个封装,它会识别当前操作系统的位数,自动返回最大的类型。
定义的代码类似于下

if LP64 || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64

typedef long NSInteger;
typedef unsigned long NSUInteger;

else

typedef int NSInteger;
typedef unsigned int NSUInteger;

endif

NSInteger与int的区别是NSInteger会根据系统的位数(32or64)自动选择int的最大数值(int or long)

NSInteger n;
n=1;
NSString *s=[NSString stringWithFormat:@"%zi",n];

"%zi"是c语言的格式化输入输出控制字符串:https://www.remlab.net/op/integer.shtml

 

 

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...