티스토리 뷰

Objective-C 2.0

3. Interface and API Design

varyeun 2022. 1. 25. 15:40

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/

 

Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS and OS X Programs

Write Truly Great iOS and OS X Code with Objective-C 2.0! Effective Objective-C 2.0 will help you harness all of Objective-C’s expressive power to write OS X or iOS code … - Selection from Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS

www.oreilly.com

 

이 책으로 공부 중이고, 기억하면 좋을 것 같은 부분을 발췌 + 공부 + 정리합니다.

 

 

하 .. 옵씨가 익숙치 않아서인지, 내가 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
댓글