Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Aspect Oriented Programming

Weizhong Yang
September 12, 2013

Aspect Oriented Programming

Weizhong Yang

September 12, 2013
Tweet

More Decks by Weizhong Yang

Other Decks in Technology

Transcript

  1. 後來,我們想確定傳⼊入的不 是 nil 或空資料 - (void)appendData:(NSData *)inData { NSParameterAssert(inData !=

    nil); NSParameterAssert([inData length]); length = [_data length]; [_data appendData:inData]; NSAssert(length != [_data length]); } Thursday, September 12,
  2. 後來,我們覺得 Release Build 也應該防⽌止輸⼊入空資料… - (void)appendData:(NSData *)inData { NSParameterAssert(inData !=

    nil); NSParameterAssert([inData length]); if (!inData) { return; } if (![inData length]) { return; } length = [_data length]; [_data appendData:inData]; NSAssert(length != [_data length]); } Thursday, September 12,
  3. 後來,我們發現這個 method 可能會在很多 thread 呼叫… - (void)appendData:(NSData *)inData { NSParameterAssert(inData

    != nil); NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); } Thursday, September 12,
  4. 後來,在 Debug Build 裡頭要 加上 TestFlight CheckPoint - (void)appendData:(NSData *)inData

    { NSParameterAssert(inData != nil); NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); #if DEBUG [TestFlight passCheckpoint:@"APPEND_DATA"]; #endif } Thursday, September 12,
  5. Release Build 則要加上 Flurry Event Log… - (void)appendData:(NSData *)inData {

    NSParameterAssert(inData != nil); NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); #if DEBUG [TestFlight passCheckpoint:@"APPEND_DATA"]; #else [Flurry logEvent:@"APPEND_DATA"]; #endif } Thursday, September 12,
  6. 還有 Google Analytics… - (void)appendData:(NSData *)inData { NSParameterAssert(inData != nil);

    NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); #if DEBUG [TestFlight passCheckpoint:@"APPEND_DATA"]; #else [Flurry logEvent:@"APPEND_DATA"]; id<GAITracker>tracker = [[GAI sharedInstance] defaultTracker]; [tracker send: [[GAIDictionaryBuilder createEventWithCategory:@"data" withAction:@"append" withLabel:@"user_data" withValue:nil] build]]; #endif } Thursday, September 12,
  7. Decorator Syntax def add(x): print "x:" + str(x) result =

    x + 1 print "result:" + str(result) return result Thursday, September 12,
  8. Decorator Syntax def log(func): def inner_func(*a): print "args:" + str(a)

    result = func(*a) print "result:" + str(result) return inner_func @log def add(x): return x + 1 Thursday, September 12,
  9. Message Forwarding - (void)forwardInvocation:(NSInvocation *)anInvocation { if ([someOtherObject respondsToSelector: [anInvocation

    selector]]) // Advice here [anInvocation invokeWithTarget:someOtherObject]; // Advice here else [super forwardInvocation:anInvocation]; } See: Objective-C Runtime Programming Guide Thursday, September 12,
  10. Objective-C 第⼀一課 • ObjC 物件都是 C Structure • ObjC method

    都是 C function pointer • 系統有⼀一個動態的索引表格,決定執⾏行 某個 selector 時,要對應到哪個 C function • Selector 就是索引表中的 key,型態為 C 字串 Thursday, September 12,
  11. Objective-C 第⼆二課 • ⼀一個 Class 有哪些 method,都是在 runtime 的時候建⽴立的 •

    所以可以在 runtime 新增新的 method, 例如使⽤用 category 語法 • 也可以直接使⽤用 runtime API 把已經存在 的 method 換掉 Thursday, September 12,
  12. <objc/runtime.h> BOOL class_addMethod(Class cls, SEL name, IMP imp, const char

    *types); IMP method_setImplementation(Method method, IMP imp); Thursday, September 12,