티스토리 뷰
Item 1: Familiarize Yourself with Objective-C's Roots
Item 2: Minimize Importing Headers in Headers
When writing an import into a header file, always ask yourself whether it’s really necessary. If the import can be forward declared, prefer that. If the import is for something used in a property, instance variable, or protocol conformance and can be moved to the class-continuation category, prefer that. Doing so will keep compile time as low as possible and reduce interdependency, which can cause problems with maintenance or with exposing only parts of your code in a public API should ever you want to do that.
// EOCPerson.h
#import <Foundation/Foundation.h>
@class EOCEmployer;
@interface EOCPerson : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, strong) EOCEmployer *employer;
@end
// EOCPerson.m
#import "EOCPerson.h"
#import "EOCEmployer.h"
@implementation EOCPerson
// Implementation of methods
@end
Always import headers at the very deepest point possible. This usually means forward declaring classes in a header and importing their corresponding headers in an implementation. Doing so avoids coupling classes together as much as possible.
Sometimes, forward declaration is not possible, as when declaring protocol conformance. In such cases, consider moving the protocol-conformance declaration to the class-continuation category, if possible. Otherwise, import a header that defines only the protocol.
Item 3: Prefer Literal Syntax over the Equivalent Methods
NSArray *animals = [NSArray arrayWithObjects:@"cat", @"dog", @"mouse", @"badger", nil];
보다는
NSArray *animals = @[@"cat", @"dog", @"mouse", @"badger"];
식으로 쓰기
cf.
NSArray나 NSDictionary는 integer과 같은 primitive type은 넣을 수가 없어서 NSNumber로 감싸줘야하는데
NSDictionary *personData =
@{@"firstName" : @"Matt",
@"lastName" : @"Galloway"
@"age" : @28};
를 봤을 때,
string이나 integer앞에 @를 붙여주면 NS 오브젝트로 감싸주는게 오브젝티브씨의 문법인 듯 하다
+ 예시 하나 더
NSMutableArray *arrayB = [@[@1, @2] mutableCopy];
Item 4: Prefer Typed Constants to Preprocessor #define
A constant that does not need to be exposed to the outside world should be defined in the implementation file where it is used.
#define 보다는 static const로 타입을 확실하게 정해줄 것
헤더에 이렇게 쓰기 보다는
#define ANIMATION_DURATION 0.3
보다는
// EOCAnimatedView.h
#import <UIKit/UIKit.h>
@interface EOCAnimatedView : UIView
- (void)animate;
@end
// EOCAnimatedView.m
#import "EOCAnimatedView.h"
static const NSTimeInterval kAnimationDuration = 0.3;
@implementation EOCAnimatedView
- (void)animate {
[UIView animateWithDuration:kAnimationDuration
animations:^(){
// Perform animations
}];
}
@end
이런 식으로 구현 파일에 써줄 것
Define global constants as external in a header file, and define them in the associated implementation file. These constants will appear in the global symbol table, so their names should be namespaced, usually by prefixing them with the class name to which they correspond.
Item 5: Use Enumerations for States, Options, and Status Codes
1.
enum EOCConnectionState {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
};
typedef enum EOCConnectionState EOCConnectionState;
EOCConnectionState state = EOCConnectionStateDisconnected;
2. bitwise OR (둘 중에 하나라도 1값이면 해당 포지션 숫자는 1이고, 둘 다 0이면 0)
enum UIViewAutoresizing {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5,
}
enum UIVewAutoresizing resizing =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
if (resizing & UIViewAutoresizingFlexibleWidth) {
// UIViewAutoresizingFlexibleWidth is set
}
3.
typedef NS_ENUM(NSUInteger, EOCEmployeeType) {
EOCEmployeeTypeDeveloper,
EOCEmployeeTypeDesigner,
EOCEmployeeTypeFinance,
};
https://www.oreilly.com/library/view/effective-objective-c-20/9780133386950/
이 책으로 공부 중이고, 기억하면 좋을 것 같은 부분을 발췌 + 공부 + 정리합니다.
'Objective-C 2.0' 카테고리의 다른 글
5. Memory Management (0) | 2022.05.10 |
---|---|
4. Protocols and Categories (0) | 2022.03.04 |
3. Interface and API Design (0) | 2022.01.25 |
2. Objects, Messaging, and the Runtime (0) | 2022.01.19 |
[Objective-C 2.0] Pointer with Objects (0) | 2022.01.14 |
- Total
- Today
- Yesterday
- 페이저타이틀스트립
- 데이터
- 전화연결하기
- subscript
- 안드로이드
- 전화걸기연결
- 프래그먼트
- Objective-C
- 스낵바설정
- 터치리스너
- 뷰페이저
- objc
- 알림대화상자
- 다이얼연결
- 카카오톡열기
- 인플레이터
- 프래그먼트매니저
- CaseIterable
- swift
- 상태드로어블
- 액션바
- ios
- 표현패턴
- 쉐이프드로어블
- 어댑터
- allcases
- prepareforreuse
- 비트맵버튼
- 부가데이터
- 제스처디텍터
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |