티스토리 뷰
Item 47: Familiarize Yourself with the System Frameworks
Item 48: Prefer Block Enumeration to for Loops
- Fast Enumeration
// Array
NSArray *anArray = /* ... */;
for (id object in anArray) {
// Do something with 'object'
}
// Dictionary
NSDictionary *aDictionary = /* ... */;
for (id key in aDictionary) {
id value = aDictionary[key];
// Do something with 'key' and 'value'
}
// Set
NSSet *aSet = /* ... */;
for (id object in aSet) {
// Do something with 'object'
}
- Block-Based Enumeration
// Array
NSArray *anArray = /* ... */;
[anArray enumerateObjectsUsingBlock:
^(id object, NSUInteger idx, BOOL *stop){
// Do something with 'object'
if (shouldStop) {
*stop = YES;
}
}];
// Dictionary
NSDictionary *aDictionary = /* ... */;
[aDictionary enumerateKeysAndObjectsUsingBlock:
^(id key, id object, BOOL *stop){
// Do something with 'key' and 'object'
if (shouldStop) {
*stop = YES;
}
}];
// Set
NSSet *aSet = /* ... */;
[aSet enumerateObjectsUsingBlock:
^(id object, BOOL *stop){
// Do something with 'object'
if (shouldStop) {
*stop = YES;
}
}];
If the objects in the dictionary are known to you as strings, you may do this:
NSDictionary *aDictionary = /* ... */;
for (NSString *key in aDictionary) {
NSString *object = (NSString*)aDictionary[key];
// Do something with 'key' and 'object'
}
NSDictionary *aDictionary = /* ... */;
[aDictionary enumerateKeysAndObjectsUsingBlock:
^(NSString *key, NSString *obj, BOOL *stop){
// Do something with 'key' and 'obj'
}];
Item 49: Use Toll-Free Bridging for Collections with Custom Memory-Management Semantics
Item 50: Use NSCache Instead of NSDictionary for Caches
The following is an example of using a cache:
#import <Foundation/Foundation.h>
// Network fetcher class
typedef void(^EOCNetworkFetcherCompletionHandler)(NSData *data);
@interface EOCNetworkFetcher : NSObject
- (id)initWithURL:(NSURL*)url;
- (void)startWithCompletionHandler:
(EOCNetworkFetcherCompletionHandler)handler;
@end
// Class that uses the network fetcher and caches results
@interface EOCClass : NSObject
@end
@implementation EOCClass {
NSCache *_cache;
}
- (id)init {
if ((self = [super init])) {
_cache = [NSCache new];
// Cache a maximum of 100 URLs
_cache.countLimit = 100;
/**
* The size in bytes of data is used as the cost,
* so this sets a cost limit of 5MB.
*/
_cache.totalCostLimit = 5 * 1024 * 1024;
}
return self;
}
- (void)downloadDataForURL:(NSURL*)url {
NSData *cachedData = [_cache objectForKey:url];
if (cachedData) {
// Cache hit
[self useData:cachedData];
} else {
// Cache miss
EOCNetworkFetcher *fetcher =
[[EOCNetworkFetcher alloc] initWithURL:url];
[fetcher startWithCompletionHandler:^(NSData *data){
[_cache setObject:data forKey:url cost:data.length];
[self useData:data];
}];
}
}
Consider using NSCache in the place of NSDictionary objects being used as caches. Caches provide optimal pruning behavior, thread safety, and don’t copy keys, unlike a dictionary.
Caches will make your applications more responsive if used correctly. Cache only data that is expensive to recalculate, such as data that needs to be fetched from the network or read from disk.
Item 51: Keep initialize and load Implementations Lean
- The load method does not participate in overriding.
- The initialize method does participate in overriding.
Item 52: Remember that NSTimer Retains Its Target
You could mandate that stopPolling(method that makes timer invalidate and assign nill to timer) be called before all other objects release an instance. However, there is no way to check for this, and if the class forms part of a public API that you expose to other developers, you cannot guarantee that they will call it.
https://www.oreilly.com/library/view/effective-objective-c-20/9780133386950/
이 책으로 공부 중이고, 기억하면 좋을 것 같은 부분을 발췌 + 공부 + 정리합니다.
드디어 .. 다 읽었다 ...
'Objective-C 2.0' 카테고리의 다른 글
6. Blocks and Grand Central Dispatch (0) | 2022.12.29 |
---|---|
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 |
- Total
- Today
- Yesterday
- 안드로이드
- ios
- 액션바
- swift
- objc
- 상태드로어블
- 터치리스너
- 카카오톡열기
- 프래그먼트
- 데이터
- 표현패턴
- 제스처디텍터
- Objective-C
- 전화연결하기
- subscript
- 쉐이프드로어블
- 어댑터
- 뷰페이저
- 스낵바설정
- CaseIterable
- 페이저타이틀스트립
- prepareforreuse
- 인플레이터
- allcases
- 프래그먼트매니저
- 부가데이터
- 다이얼연결
- 전화걸기연결
- 알림대화상자
- 비트맵버튼
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |