Skip to content

Instantly share code, notes, and snippets.

@nek023
Created June 6, 2014 08:08
Show Gist options
  • Save nek023/ed84f124094fac3ee33e to your computer and use it in GitHub Desktop.
Save nek023/ed84f124094fac3ee33e to your computer and use it in GitHub Desktop.
Immutable user class in Objective-C and Swift
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic, copy, readonly) NSString *name;
- (instancetype)initWithName:(NSString *)name;
@end
#import "User.h"
@interface User ()
@property (nonatomic, copy, readwrite) NSString *name;
@end
@implementation User
- (instancetype)initWithName:(NSString *)name
{
self = [super init];
if (self) {
self.name = name;
}
return self;
}
@end
import Cocoa
class User: NSObject {
let name: String
init(name: String) {
self.name = name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment