On 30 Jan 2017, at 10:12, Torin Kwok <to...@kwok.im> wrote:

> In Obj-C, if a property has promised that it conforms to <NSCopying>
> porotocl and that it would respect copying semantic by being qualified
> with `@property (copy)`, then we assign a value to `ivar` through the
> setter by writting down `self.ivar = whatever` in `-init` …

You can do that if you choose to, but the Objective-C convention is to use 
direct ivar access in `-init` and `-dealloc`.  This is explicitly called out in 
the “Access Instance Variables Directly from Initializer Methods” section of 
“Programming with Objective-C”, which says:

> You should always access the instance variables directly from within an 
> initialization method …

<https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW11>

For an object with a copyable property `name`, the `-init` method would look 
like this:

- (instancetype)initWithName:(NSString *)name {
    self = [super init];
    if (self != nil) {
        self->_name = [name copy];
    }
    return self;
}

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to