Copy property on Objective-C
Last active
February 20, 2016 11:10
-
-
Save oney/d0033841b9ee7ccc133a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface CustomObject : NSObject <NSCopying> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "CustomObject.h" | |
@implementation CustomObject | |
- (instancetype)copyWithZone:(NSZone *)zone { | |
CustomObject *copy = [[[self class] allocWithZone:zone] init]; | |
return copy; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "CustomObject.h" | |
@interface Model : NSObject | |
@property (nonatomic, readwrite, copy) CustomObject *object; | |
@property (nonatomic, readwrite, copy) NSArray *array; | |
@property (nonatomic, readwrite, copy) NSMutableArray *mutableArray; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Model.h" | |
@implementation Model | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
_object = [CustomObject new]; | |
_array = [[NSArray alloc] initWithObjects:@"foo", @"bar", nil]; | |
_mutableArray = [[NSMutableArray alloc] initWithObjects:@"foo", @"bar", nil]; | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment