Created
April 1, 2013 13:52
-
-
Save sora0077/5285064 to your computer and use it in GitHub Desktop.
iOS開発でよく使うマクロ数種 ref: http://qiita.com/items/154f8d39f7189f22daa7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // メッセージを続けて記述可能 | |
| // [SafeCast(NSMutableURLRequest, request) setTimeoutInterval:30]; | |
| #define SafeCast(type, var) \ | |
| ^type *(id obj, Class clazz) {\ | |
| if ([obj isKindOfClass:clazz]) {\ | |
| return obj;\ | |
| }\ | |
| return nil;\ | |
| } (var, [type class]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 第一引数にブロック本体を、続けてブロックに渡す引数を記述 | |
| // SafeBlockHandler(completion, arg0, arg1); // == completion(arg0, arg1); | |
| #define SafeBlockHandler(block, ...) \ | |
| do {\ | |
| if (block) {\ | |
| block(__VA_ARGS__);\ | |
| }\ | |
| } while (0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 実際はこのマクロを直接利用せず、型ごとにインライン関数を定義してそこから利用している | |
| // e.g. | |
| // static inline void NSUserDefaultsWriteObject(id aKey, id anObject) | |
| // { | |
| // NSUserDefaults_Write(aKey, anObject, setObject); | |
| // } | |
| // static inline id NSUserDefaultsReadObject(id aKey) | |
| // { | |
| // NSUserDefaults_Read(aKey, objectForKey); | |
| // } | |
| #define NSUserDefaults_Write(aKey, anObject, method) \ | |
| NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\ | |
| if (anObject) {\ | |
| [defaults method:anObject forKey:aKey];\ | |
| } else {\ | |
| [defaults removeObjectForKey:aKey];\ | |
| }\ | |
| [defaults synchronize] | |
| #define NSUserDefaults_Read(aKey, method) \ | |
| NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];\ | |
| return [defaults method:aKey] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment