zoukankan      html  css  js  c++  java
  • objective-c arc -对象、变量、变量修饰符、赋值

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

    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

  • 相关阅读:
    Unity HOME
    Unity Document
    css3动画的性能优化_针对移动端卡顿问题
    前端工程师分享几个CSS技巧
    什么是前端? web1.0、web2.0时代的网页制作,前端开发都有哪些内容等
    前端工程之CDN部署_前端为什么非要动静分离?说一下CDN托管的意义
    JSON API免费接口_ 免费的天气预报、地图、IP、手机信息查询、翻译、新闻等api接口
    利用伪元素:before和:after插入图标
    CSS中的三大特性_继承性、层叠性、优先级
    WEB专用服务器的安全设置的实战技巧----IIS设置
  • 原文地址:https://www.cnblogs.com/feng9exe/p/13840815.html
Copyright © 2011-2022 走看看