zoukankan      html  css  js  c++  java
  • copy 和 strong(或retain)的区别

    http://stackoverflow.com/questions/18526909/whether-i-should-use-propertynonatomic-copy-or-propertynonatomic-strong-fo

    'copy' will cause the setter for that property to create a copy of the object, and is otherwise identical to strong. You would use this to make sure that if someone sets your property to a mutable string, then mutates the string, you still have the original value. If the string isn't mutable, Cocoa will silently optimize out the copy operation, which is nice :)

    'strong' will keep the property's value alive until it's set to something else. If you want incoming mutable strings to change out from under you (not impossible, but not all that common, a thing to want), then strong would be the right thing to do. Generally strong is more useful for objects that represent something more complex than a simple "value" (i.e. not NSString, NSNumber, NSValue, etc...).

    'assign' is the default (and indeed only) possible setting for an integer. Integers can't be retained or copied like objects.

    For attributes whose type is an immutable value class that conforms to the NSCopying protocol, you almost always should specify copy in your @property declaration. Specifying retain is something you almost never want in such a situation.In non ARC strong will work like retain

    Here's why you want to do that:

    NSMutableString *someName = [NSMutableString stringWithString:@"Chris"];
    Person *p = [[[Person alloc] init] autorelease];
    p.name = someName;
    [someName setString:@"Debajit"];

    The current value of the Person.name property will be different depending on whether the property is declared retain or copy — it will be @"Debajit" if the property is marked retain, but @"Chris" if the property is marked copy.

    Since in almost all cases you want to prevent mutating an object's attributes behind its back, you should mark the properties representing them copy. (And if you write the setter yourself instead of using @synthesize you should remember to actually use copy instead of retain in it.)

  • 相关阅读:
    uniapp、小程序之swiperitem内容过多显示不全的解决方案
    PingFang(苹方)字体的引用
    Vue项目中使用websocket
    uniapp页面跳转传递参数过长
    uniapp开发的h5,使用微信授权登录(前置条件+具体代码)
    Maven工具安装使用
    Lombok代码生成插件使用
    对不起,我的文章暂时无法公开!
    [iOS]学习笔记7(CATransform3DFlip效果)
    [iOS]学习笔记8 (iOS之阻塞)
  • 原文地址:https://www.cnblogs.com/jay-dong/p/4126324.html
Copyright © 2011-2022 走看看