• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c arc -对象、变量、变量修饰符、赋值

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

对象、变量、变量修饰符、赋值

 

1、站在对象和引用计数的角度看:我不关心谁拥有我,我只关心谁想我发出了维护消息;

[_dog release];

2、任何变量的赋值,都代表了内存规则的进一步维护;

 

 

引用计数的语义是什么?

指针,内存变量、对象

 

strong、retain、release:向对象发送消息,修改引用计数的值;

赋值操作:使用变量修饰符对内存维护机制发送维护信息;

 

Variable Qualifiers

You use the following lifetime qualifiers for variables just like you would, say, const.

 

  • __strong is the default. An object remains “alive” as long as there is a strong pointer to it.
  • __weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.
  • __unsafe_unretained specifies a reference that does not keep the referenced object alive and is not set to nil when there are no strong references to the object. If the object it references is deallocated, the pointer is left dangling.
  • __autoreleasing is used to denote arguments that are passed by reference (id *) and are autoreleased on return.

https://developer.apple.com/library/archive/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

 

You don't have to explicitly specify __autoreleasing when defining a function that returns an object, for example

-(BOOL)doSomething:(NSError **)error;

The ARC compiler automatically inserts the __autoreleasing. This is explained in the Clang/ARC documentation:

4.4.2 Indirect parameters

If a function or method parameter has type T*, where T is an ownership-unqualified retainable object pointer type, then:

  • if T is const-qualified or Class, then it is implicitly qualified with __unsafe_unretained;
  • otherwise, it is implicitly qualified with __autoreleasing.

The Xcode code completion also knows about that and displays (NSError *__autoreleasing *)error.

 

When calling such a function the ARC compiler also automatically does "the right thing", so you can just call

NSError *error;

BOOL success = [self doSomething:&error];

As explained in the "Transitioning to ARC Release Notes", the compiler inserts a temporary __autoreleasing variable:

NSError *error;

NSError * __autoreleasing tmp = error;

BOOL success = [self doSomething:&tmp];

error = tmp;

(For the gory details you can read 4.3.4 "Passing to an out parameter by writeback" in the Clang/ARC documentation.)

 

https://stackoverflow.com/questions/23268576/autoreleasing-in-errornserror-autoreleasing-outerror

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-CNSTableView重点知识汇总发布时间:2022-07-12
下一篇:
Objective - C 之协议发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap