社区
iOS
帖子详情
关于initWithFormat方法,为什么这两个方法返回的不一样啊,谢谢大神指点
snelost
2017-11-19 01:12:40
...全文
292
1
打赏
收藏
关于initWithFormat方法,为什么这两个方法返回的不一样啊,谢谢大神指点
[图片]
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
不担心
2017-11-21
打赏
举报
回复
@"def"是个字符串对象,同样通过initwithformat初始化出的@"abc%@"也是个字符串对象,而str1就是指向这个字符串的指针 到了str2,根据编译器的优化,系统能识别出这里的@"abc%@"出来的字符串和str1是同一个对象,所以只是将这个对象的计数器+1,并把str2和str1设置为指向同一个地址的指针 而str11在通过initwithformat初始化时,需要在占位符那里填充一个字符串对象,而函数里提供的1234567890并不是对象,是基础类型,是个值,所以只能通过装箱把124567890转换为字符串对象,这里就会产生一个新的对象,也就是说他有自己的内存地址,同样道理str12也会产生一个新的对象,有自己的地址,所以str11和str21指向的地址并不相同,所以str11 != str21 没有仔细去研究,不过基本上应该差不太远,其实很多问题只要明白基础数据类型和对象的区别就很容易明白
init
With
Format
和stringWith
Format
的区别
差别: 1、
init
With
Format
是实例办法 只能经由过程 NSString* str = [[NSString alloc]
init
With
Format
:@"%@",@"Hello World"] 调用,然则必须手动release来开释内存资料 2、stringWith
Format
是类办法 可以直接用 NSString* str = [NSString strin
MRC下的
init
With
Format
: 和 stringWith
Format
:
Both of these methods work equally well. You just have to know how to use them properly. "
init
With
Format
" is an instance method that should be applied to a newly alloc-ed instance of NSString, which
init
With
Format
与
init
WithString的区别、
init
With
Format
与stringWith
Format
的区别
一、
init
With
Format
会创建新空间,NSLog时可以“%d”。
init
WithString不一定创建新空间,取决于参数。 (1)如果后边是常量字符串不会创建新空间,NSLog("%d",[str retainCount])时输出为-1,NSlog("%u",[str retainCount])时输出为一串很长的数。 (2)如果是定义新字符串,就会创建新空间。 二、下边是转载的,
ios:
init
WithString和
init
With
Format
的区别
差别: 1、
init
With
Format
是实例办法 只能经由过程 NSString* str = [[NSString alloc]
init
With
Format
:@"%@",@"Hello World"] 调用,然则必须手动release来开释内存资料 2、stringWith
Format
是类办法 可以直接用 NSString* str = [NSString strin
iOS: NSString的
方法
init
With
Format
:locale:arguments:
- (id)
init
With
Format
:(NSString *)
format
locale:(id)locale arguments:(va_list)argList va_list myArgs; NSString *myString = [[NSString alloc]
iOS
29,027
社区成员
12,467
社区内容
发帖
与我相关
我的任务
iOS
主要讨论与iOS相关的软件和技术
复制链接
扫一扫
分享
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章