Skip to content

Instantly share code, notes, and snippets.

@allenlinli
Last active October 12, 2016 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenlinli/d9a9400fda28809c914c87bfa50575e0 to your computer and use it in GitHub Desktop.
Save allenlinli/d9a9400fda28809c914c87bfa50575e0 to your computer and use it in GitHub Desktop.
Please read the README in front of this code.
/*
README:
Updated on 13/Oct. I can use self.favoriteList in .m file. Then we can use custom getter here.
1. The implemetation I did for the first time was correct. I used "@property (atomic) NSMutableArray<NSString *> *favoriteList;" in @interface Booking() and it works.
2. I don't need "//@synthesize favoriteList = _favoriteList;" for creating setter and getter, becuase Xcode will create them automaticlly now.
3. I think my implementation is better than having a "@property (atomic) NSMutableArray<NSString *> *favoriteList2;" in .m. Becuase having a favoriteList2 may confuse people. The cons of my implementation is that you may create custom getter and setter so you don't want to use _favoriteList, but for this simple example it should be fine.
*/
//
// Booking.h
// test
//
// Created by allenlinli on 10/11/16.
// Copyright © 2016 Raccoonism. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Booking : NSObject
@property (atomic, readonly) NSArray *favoriteList;
- (void)addFavorite:(NSString *)cityName;
@end
//
// Booking.m
// test
//
// Created by allenlinli on 10/11/16.
// Copyright © 2016 Raccoonism. All rights reserved.
//
#import "Booking.h"
@interface Booking()
@property (atomic) NSMutableArray<NSString *> *favoriteList;
// Li Lin: I don't need to use favoriteList2 for implementing this functionality.
//@property (atomic) NSMutableArray<NSString *> *favoriteList2;
@end
@implementation Booking
- (instancetype)init {
self = [super init];
if (self) {
// [self setFavoriteList: [[NSMutableArray alloc] init]];
_favoriteList = [[NSMutableArray alloc] init];
}
return self;
}
//- (NSMutableArray *)favoriteList {
// if (!_favoriteList) {
// _favoriteList = [[NSMutableArray alloc] init];
// }
//
// return _favoriteList;
//}
//
//- (void)setFavoriteList:(NSMutableArray *)favoriteList {
// self.favoriteList = favoriteList;
//}
- (void)addFavorite:(NSString *)cityName {
// [_favoriteList addObject:cityName];
[self.favoriteList addObject:cityName];
}
// Li Lin: "synthesize" is automatically produced
//@synthesize favoriteList = _favoriteList;
@end
/*
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// test
//
// Created by allenlinli on 10/11/16.
// Copyright © 2016 Raccoonism. All rights reserved.
//
#import "ViewController.h"
#import "Booking.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Booking *book = [[Booking alloc] init];
[book addFavorite:@"Seoul"];
[book addFavorite:@"Taipei"];
[book addFavorite:@"Darwin"];
// book.favoriteList = nil;
for (NSString *city in book.favoriteList) {
NSLog(@"city:%@",city);
}
}
@end
*/
@allenlinli
Copy link
Author

Updated on 13/Oct. I can use self.favoriteList in .m file. Then we can use custom getter here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment