티스토리 뷰

Objective-C 2.0

7. The System Frameworks

varyeun 2023. 1. 3. 17:00

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/

 

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

 

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

 

 

드디어 .. 다 읽었다 ...

'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
댓글