• strong、weak、unsafe_unretained(ARC);retain


    property with 'weak' attribute must be of object type:

    I have this in my interface:

    @property(nonatomic, weak)NSTimeInterval*timeStamp;
    Or:
    @property (nonatomic, weak) float height;

    Which my logic told me, I need a time stamp object, that only is going to be used by this class within the context of its instantiation, so "weak" seemed to be logical to me-- but XCode tells me "property with 'weak' attribute must be of object type"... If I just do:

    @property(nonatomic)NSTimeInterval*timeStamp;

    Then the error goes away, but I am not sure I understand why...

    The problem is that NSTimeInterval is a value type -- it's an alias for double, essentially (check NSDate.h for the typedef). The weak attribute only applies to objects that have a retain count (that is, anything that descends from NSObject or NSProxy).

    As such, storing a pointer to NSTimeInterval is probably a mistake on your part. You will most likely never receive a pointer to an NSTimeInterval unless you're expected to write to a given address as an output to a function (probably a callback in such a case). That said, I'm not aware of any functions with NSTimeInterval * as a return type nor any that pass the same to a callback.

    ----------------------------------------------------------------------------------------------------------------

    I'm still new to Objective-C and having some difficulties trying to figure out the appropriate way to use assign, retain, copy, strong, etc. when setting a property.

    For example, I have the following types declared - how should I be setting the properties?

    @property(nonatomic,??)NSMutableArray*myArray
    @property(nonatomic,??)NSString*myString
    @property(nonatomic,??)UIColor*myColor
    @property(nonatomic,??)int*myIn
    @property(nonatomic,??) BOOL *myBOOL

    Thanks....

    To reiterate, it does depend on context. In an non-ARC situation:

    @property(nonatomic, copy)NSMutableArray*myArray
    @property(nonatomic, copy)NSString*myString
    @property(nonatomic, retain)UIColor*myColor
    //Note the change to an int rather than a pointer to an int
    @property(nonatomic, assign)int myInt //Note the change to an int rather than a pointer to an int
    @property(nonatomic, assign) BOOL myBOOL

    The copy on myArray is to prevent modification by another "owner" of the object you set. In an ARC project, things change a bit:

    @property(nonatomic, copy)NSMutableArray*myArray
    @property(nonatomic, copy)NSString*myString
    @property(nonatomic, strong)UIColor*myColor
    //Note the change to an int rather than a pointer to an int
    @property(nonatomic, assign)int myInt //Note the change to an int rather than a pointer to an int
    @property(nonatomic, assign) BOOL myBOOL

    The change is primarily to myColor in your situation. You wouldn't use retain as you aren't managing reference counting directly. The strong keyword is a way of asserting "ownership" of the property and similar to retain. An additional keyword is also provided, weak, that would typically be used in place of assign for object types. Apple's common example of a weak property is for delegates. I'd recommend going through the Transitioning to ARC Release Notes in addition to the Memory Management Guide a time or two as there is more nuance than can easily be covered in an SO post.

    ----------------------------------------------------------------------------------------------------------------

    i'm a little confused about these two qualifiers... With ARC instead of use weak (i.e. if i need support iOS 4) i can use unsafe_unretained losing the auto-nil features... the final result seems to be similar to assign.

    • Can i exchange unsafe_unretained with assign ?
    • Are these qualifiers the same thing ?

    Clang's technical specification of ARC goes into much more detail about how the qualifiers work.

    But, to answer your question: assign and __unsafe_unretained are not the same thing. assignis a property attribute that tells the compiler how to synthesise the property's setter implementation, while __unsafe_unretained is an ownership(所有权) qualifier(限定符) that tells ARC how to insert retain/release calls. But they are related: when declaring a property, assign implies__unsafe_unretained ownership, and since assign is the default @property setter semantic, forgetting to include either doesn't change the code generated.

    http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

  • 相关阅读:
    tyflow birth节点
    tyflow雨滴在物体上滑落测试
    【bootstrap】如何在js中动态修改提示冒泡(Tooltips)的显示内容
    解决office自动更新失败,错误代码0xc0000142
    hosts文件路径(Windows)
    【微信测试版】支持安卓平板和手机同时登录
    【javascript】canvas画布涂鸦及保存图片到本地
    【python】图片批量压缩(多线程)
    【python】提取pdf文件中的所有图片
    【python】计算程序运行所消耗的总时间
  • 原文地址:https://www.cnblogs.com/mumue/p/3050015.html
Copyright © 2020-2023  润新知