티스토리 뷰
Item 15: Use Prefix Names to Avoid Namespace Clashes
Item 16: Have a Designated Initializer
Item 17: Implement the description Method
- (NSString*)description {
return [NSString stringWithFormat:@"<%@: %p, \"%@ %@\">",
[self class], self, _firstName, _lastName];
}
If this were to be used, the output for an object of type EOCPerson would now look like the following:
EOCPerson *person = [[EOCPerson alloc]
initWithFirstName:@"Bob"
lastName:@"Smith"];
NSLog(@"person = %@", person);
// Output:
// person = <EOCPerson: 0x7fb249c030f0, "Bob Smith">
Item 18: Prefer Immutable Objects
if mutable objects are stored in collections, a set’s internal data structures can easily become inconsistent if objects held in the set are mutated. Therefore, I recommend making objects only as mutable as they need to be.
NSMutableSet *set = [NSMutableSet new];
NSMutableArray *arrayA = [@[@1, @2] mutableCopy];
[set addObject:arrayA];
NSLog(@"set = %@", set);
// Output: set = {((1,2))}
NSMutableArray *arrayB = [@[@1, @2] mutableCopy];
[set addObject:arrayB];
NSLog(@"set = %@", set);
// Output: set = {((1,2))}
NSMutableArray *arrayC = [@[@1] mutableCopy];
[set addObject:arrayC];
NSLog(@"set = %@", set);
// Output: set = {((1),(1,2))}
[arrayC addObject:@2];
NSLog(@"set = %@", set);
// Output: set = {((1,2),(1,2))}
NSSet *setB = [set copy];
NSLog(@"setB = %@", setB);
// Output: setB = {((1,2))}
이렇게 되어버릴 수 있어서 최대한(가능한) immutable 데이터 타입을 쓰는게 좋음
근데 mutable처럼 사용해야한다면 ?
-> 이런 식으로 사용하면 좋음
// EOCPerson.h
#import <Foundation/Foundation.h>
@interface EOCPerson : NSObject
@property (nonatomic, copy, readonly) NSString *firstName;
@property (nonatomic, copy, readonly) NSString *lastName;
@property (nonatomic, strong, readonly) NSSet *friends;
- (id)initWithFirstName:(NSString*)firstName
andLastName:(NSString*)lastName;
- (void)addFriend:(EOCPerson*)person;
- (void)removeFriend:(EOCPerson*)person;
@end
// EOCPerson.m
#import "EOCPerson.h"
@interface EOCPerson ()
@property (nonatomic, copy, readwrite) NSString *firstName;
@property (nonatomic, copy, readwrite) NSString *lastName;
@end
@implementation EOCPerson {
NSMutableSet *_internalFriends;
}
- (NSSet*)friends {
return [_internalFriends copy];
}
- (void)addFriend:(EOCPerson*)person {
[_internalFriends addObject:person];
}
- (void)removeFriend:(EOCPerson*)person {
[_internalFriends removeObject:person];
}
- (id)initWithFirstName:(NSString*)firstName
andLastName:(NSString*)lastName {
if ((self = [super init])) {
_firstName = firstName;
_lastName = lastName;
_internalFriends = [NSMutableSet new];
}
return self;
}
@end
Provide methods to mutate collections held by objects rather than exposing a mutable collection as a property.
cf.
계속 objc에서 굳이 selector를 쓰는 이유가 뭔지 모르겠어서 여러 레퍼런스를 많이 찾아봤는데
swift에서와 마찬가지로 selector를 잘 이용하면 실행시에 호출해야 할 메소드를 상황에 따라 바꾸는 프로그램을 구현할 수 있게 된다.
Item 19: Use Clear and Consistent Naming
- (id)initWithWidth:(float)width andHeight:(float)height;
EOCRectangle *aRectangle =
[[EOCRectangle alloc] initWithWidth:5.0f andHeight:10.0f];
Don’t be afraid to use long method names.
Item 20: Prefix Private Method Names
Item 21: Understand the Objective-C Error Model
Item 22: Understand the NSCopying Protocol
-[NSMutableArray copy] => NSArray
-[NSArray mutableCopy] => NSMutableArray
If such a deep copy were required, you could provide a method such as the following:
- (id)deepCopy {
EOCPerson *copy = [[[self class] alloc]
initWithFirstName:_firstName
andLastName:_lastName];
copy->_friends = [[NSMutableSet alloc] initWithSet:_friends
copyItems:YES];
return copy;
}
https://www.oreilly.com/library/view/effective-objective-c-20/9780133386950/
이 책으로 공부 중이고, 기억하면 좋을 것 같은 부분을 발췌 + 공부 + 정리합니다.
하 .. 옵씨가 익숙치 않아서인지, 내가 CS개념을 잘 모르는건지 너무 어렵다 ㅠㅠㅠ
'Objective-C 2.0' 카테고리의 다른 글
5. Memory Management (0) | 2022.05.10 |
---|---|
4. Protocols and Categories (0) | 2022.03.04 |
2. Objects, Messaging, and the Runtime (0) | 2022.01.19 |
1. Accustoming Yourself to Objective-C (0) | 2022.01.17 |
[Objective-C 2.0] Pointer with Objects (0) | 2022.01.14 |
- Total
- Today
- Yesterday
- swift
- 스낵바설정
- 표현패턴
- subscript
- 카카오톡열기
- 비트맵버튼
- 제스처디텍터
- 액션바
- prepareforreuse
- 프래그먼트매니저
- Objective-C
- 뷰페이저
- 다이얼연결
- 쉐이프드로어블
- 부가데이터
- 안드로이드
- 알림대화상자
- 프래그먼트
- ios
- allcases
- 어댑터
- 데이터
- 전화연결하기
- 터치리스너
- 상태드로어블
- objc
- 전화걸기연결
- CaseIterable
- 인플레이터
- 페이저타이틀스트립
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |