Instantly share code, notes, and snippets.

Embed
What would you like to do?
Property: What's the difference between strong and copy. Blogpost: http://stackenblochen.blogsport.eu/2014/02/25/property-strong-and-copy/
//
// main.m
// strong_retain_copy
//
// Created by Christopher Loessl on 25/02/14.
// Copyright (c) 2014 Christopher Loessl. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (strong, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *nick;
@end
@implementation Person
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person *per = [[Person alloc] init];
NSMutableString *aName = [NSMutableString stringWithString:@"Christopher"];
// The pointer "per.name" is changed.
// "per.name" points at the same location as "someName" afterwards
per.name = aName;
// This is equal to
// per.address = [someName copy];
// Therefore the points are not the same afterwards
per.nick = aName;
// Changing "someName" to
[aName setString:@"hashier"];
NSLog(@"per.name : %@", per.name);
NSLog(@"per.nick : %@", per.nick);
NSLog(@"someName : %@", aName);
NSLog(@"\nPointer 1: %p\nPointer 2: %p\nPointer 3: %p", per.name, per.nick, aName);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment