Skip to content

Instantly share code, notes, and snippets.

@hashier
Created February 25, 2014 21:23
Show Gist options
  • Save hashier/9218108 to your computer and use it in GitHub Desktop.
Save hashier/9218108 to your computer and use it in GitHub Desktop.
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