Skip to content

Instantly share code, notes, and snippets.

@oney
Last active February 20, 2016 11:10
Show Gist options
  • Save oney/d0033841b9ee7ccc133a to your computer and use it in GitHub Desktop.
Save oney/d0033841b9ee7ccc133a to your computer and use it in GitHub Desktop.

Copy property on Objective-C

#import <Foundation/Foundation.h>
@interface CustomObject : NSObject <NSCopying>
@end
#import "CustomObject.h"
@implementation CustomObject
- (instancetype)copyWithZone:(NSZone *)zone {
CustomObject *copy = [[[self class] allocWithZone:zone] init];
return copy;
}
@end
#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
#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