IOS高级开发~底层实现资源收集

 

[置顶] IOS高级开发~底层实现资源收集

分类: IOS C&C++   506人阅读  评论(0)  收藏  举报

注意:dlopen()一个路径时候要以你本机的路径为准,我的路径为(首先右键你的XCode,然后查看Content):

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation

1、NSArray & NSAutoreleasePool 实现

[cpp]  view plain copy print ?
  1. #include <objc/runtime.h>  
  2. #include <dlfcn.h>  
  3. #include <stdio.h>  
  4.    
  5. int main() {  
  6.    
  7.   void *sdl_library = dlopen("/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation", RTLD_LAZY);  
  8.   if (sdl_library == NULL) {  
  9.     printf("Got dlopen error!\n");  
  10.   } else {  
  11.     printf("dlopen successful!\n");  
  12.    
  13.     // Set up an NSAutoreleasePool  
  14.     Class nsautoreleasepool = objc_getClass("NSAutoreleasePool");  
  15.     id pool = class_createInstance(nsautoreleasepool, 0);  
  16.     SEL initSel = sel_registerName("init");  
  17.     id poolAfterInit = objc_msgSend(pool, initSel);  
  18.    
  19.     // Create an NSMutableArray  
  20.     Class nsmutablearray = objc_getClass("NSMutableArray");  
  21.    
  22.     // Regular way to create an NSMutableArray,  
  23.     // equivalent to [[NSMutableArray alloc] initWithCapacity: 10]  
  24.     id array = objc_msgSend(nsmutablearray, sel_registerName("alloc"));  
  25.     id arrayAfterInit = objc_msgSend(array, sel_registerName("initWithCapacity:"), 10);  
  26.    
  27.     // Alternative, shorthand way of creating an NSMutableArray,  
  28.     // equivalent to [NSMutableArray arrayWithCapacity: 10]  
  29.     //id arrayAfterInit = objc_msgSend(nsmutablearray, sel_registerName("arrayWithCapacity:"), 10);  
  30.    
  31.     // Add an NSString and the nsma class reference to the array  
  32.     objc_msgSend(arrayAfterInit, sel_registerName("addObject:"), @"test");  
  33.     objc_msgSend(arrayAfterInit, sel_registerName("addObject:"), nsmutablearray);  
  34.    
  35.     // Print out the length and debug printout  
  36.     NSLog(@"%d", objc_msgSend(arrayAfterInit, sel_registerName("count")));  
  37.     NSLog(@"%@", arrayAfterInit);  
  38.   }  
  39.    
  40.   return 0;  
  41. }  



2、Block

(一)

<div class="line" id="file-block-m-LC1"><div class="dp-highlighter bg_cpp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 824.65625px; overflow: auto; padding-top: 1px; opacity: 0.6; margin: 18px 0px !important; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/cpp.gif); background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: 100% 0%; background-repeat: no-repeat;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108);"><strong>[cpp]</strong> <a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><div style="position: absolute; left: 520px; top: 1411px; width: 29px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#import <Foundation/Foundation.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <objc/runtime.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <Block.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <dlfcn.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <stdio.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> main() {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *sdl_library = dlopen(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, RTLD_LAZY);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (sdl_library == NULL) {  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"Got dlopen error!\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  } <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">else</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> {  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"dlopen successful!\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Set up an NSAutoreleasePool</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Class nsautoreleasepool = objc_getClass(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"NSAutoreleasePool"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id pool = class_createInstance(nsautoreleasepool, 0);  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    SEL initSel = sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"init"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id poolAfterInit = objc_msgSend(pool, initSel);  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Create an NSMutableArray</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Class nsmutablearray = objc_getClass(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"NSMutableArray"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Regular way to create an NSMutableArray,</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// equivalent to [[NSMutableArray alloc] initWithCapacity: 10]</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id array = objc_msgSend(nsmutablearray, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"alloc"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">));  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id arrayAfterInit = objc_msgSend(array, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"initWithCapacity:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), 10);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"a"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"b"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"c"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"d"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"e"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"f"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"g"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"h"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"i"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"j"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"k"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"l"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id b = ^(id x, NSUInteger index, <span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">BOOL</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *stop){  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      NSLog(@<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"%@"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, x);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    };  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    NSLog(@<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"%@"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, b);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"enumerateObjectsUsingBlock:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), b);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  }  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 0;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li></ol></div></div>

(二)

<div class="line" id="file-block2-m-LC1"><div class="dp-highlighter bg_cpp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 880.109375px; overflow: auto; padding-top: 1px; opacity: 0.6; margin: 18px 0px !important; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/cpp.gif); background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: 100% 0%; background-repeat: no-repeat;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108);"><strong>[cpp]</strong> <a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><div style="position: absolute; left: 520px; top: 2483px; width: 29px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#import <Foundation/Foundation.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <objc/runtime.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <Block.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <dlfcn.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <stdio.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_literal_1 {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *isa;  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> flags;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> reserved;   </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (*invoke)(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_literal_1 *, id obj, NSUInteger idx, </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">BOOL</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *stop);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_descriptor_1 *descriptor;  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">};  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// This is the regular C function we are turning into a Block.</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_invoke_1(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_literal_1 *_block, id obj, NSUInteger idx, </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">BOOL</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *stop) {  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  NSLog(@<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"%d: %@"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, idx, obj);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">static</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_descriptor_1 {  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  unsigned <span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">long</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> reserved;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  unsigned <span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">long</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Block_size;  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} __block_descriptor_1 = { 0, <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_literal_1) };  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> main() {  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"sizeof(int): %lu\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">));  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"sizeof(void *): %lu\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *));  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"sizeof(void (*)()): %lu\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (*)()));  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"sizeof(struct __block_descriptor *): %lu\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_descriptor *));  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"sizeof(__block_literal_1): %lu\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_literal_1));  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"sizeof(__block_descriptor_1): %lu\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_descriptor_1));  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">" calculated: %lu\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">*)+</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)+</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)+</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (*)())+</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">sizeof</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_descriptor_1 *));  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *sdl_library = dlopen(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, RTLD_LAZY);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (sdl_library == NULL) {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"Got dlopen error!\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  } <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">else</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"dlopen successful!\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Set up an NSAutoreleasePool</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Class nsautoreleasepool = objc_getClass(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"NSAutoreleasePool"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id pool = class_createInstance(nsautoreleasepool, 0);  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    SEL initSel = sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"init"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id poolAfterInit = objc_msgSend(pool, initSel);  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Create an NSMutableArray</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Class nsmutablearray = objc_getClass(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"NSMutableArray"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Regular way to create an NSMutableArray,</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// equivalent to [[NSMutableArray alloc] initWithCapacity: 10]</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id array = objc_msgSend(nsmutablearray, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"alloc"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">));  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id arrayAfterInit = objc_msgSend(array, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"initWithCapacity:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), 10);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"a"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"b"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"c"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"d"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"e"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"f"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"g"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"h"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"i"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"j"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"k"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"addObject:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), @</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"l"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> __block_literal_1 _block_literal = {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      &_NSConcreteStackBlock,  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//&_NSConcreteGlobalBlock,</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      (1<<29),  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      0,  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      __block_invoke_1,  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">      &__block_descriptor_1  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    };  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id b = (id)&_block_literal;  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    NSLog(@<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"%@"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, b);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    objc_msgSend(arrayAfterInit, sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"enumerateObjectsUsingBlock:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">), b);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  }  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 0;  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li></ol></div></div>

3、打印Class详情

[cpp]  view plain copy print ?
  1. <span style="font-size:12px;">#include <objc/runtime.h>  
  2. #include <dlfcn.h>  
  3. #include <stdio.h>  
  4.    
  5. int main(int argc, char **argv) {  
  6.    
  7.   dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", RTLD_LAZY);  
  8.   dlopen("/System/Library/Frameworks/AppKit.framework/AppKit", RTLD_LAZY);  
  9.   dlopen("/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY);  
  10.    
  11.   char * className = argc == 2 ? argv[1] : "NSMutableArray";  
  12.   Class NSMA = objc_getClass(className);  
  13.    
  14.   if (!NSMA) {  
  15.     printf("Could not load Class: %s\n", className);  
  16.     return 1;  
  17.   }  
  18.    
  19.   const char *name = class_getName(NSMA);  
  20.   printf("class_getName: %s\n", name);  
  21.    
  22.   int version = class_getVersion(NSMA);  
  23.   printf("class_getVersion: %d\n", version);  
  24.    
  25.   const char *weakIvarLayout = class_getWeakIvarLayout(NSMA);  
  26.   printf("class_getWeakIvarLayout: %s\n", weakIvarLayout);  
  27.    
  28.   BOOL isMetaClass = class_isMetaClass(NSMA);  
  29.   printf("class_isMetaClass: %s\n", isMetaClass ? "yes" : "no");  
  30.    
  31.   // get the metaclass  
  32.   id metaclass = NSMA->isa;  
  33.   isMetaClass = class_isMetaClass(metaclass);  
  34.   printf("class_isMetaClass(isa): %s\n", isMetaClass ? "yes" : "no");  
  35.    
  36.    
  37.   // Look up the Ivars  
  38.   unsigned int i = 0;  
  39.   unsigned int numIvars = 999999;  
  40.   Ivar *ivars = NULL;  
  41.   ivars = class_copyIvarList(NSMA, &numIvars);  
  42.   printf("\nNum ivars: %d\n", numIvars);  
  43.   for (; i<numIvars; i++) {  
  44.     Ivar ivar = ivars[i];  
  45.     const char *ivarName = ivar_getName(ivar);  
  46.     printf("  %s (%s)\n", ivarName, ivar_getTypeEncoding(ivar));  
  47.   }  
  48.   free(ivars);  
  49.    
  50.    
  51.   // Copy the Property List  
  52.   unsigned int numProps = 999999;  
  53.   objc_property_t *props = NULL;  
  54.   props = class_copyPropertyList(NSMA, &numProps);  
  55.   printf("\nNum props: %d\n", numProps);  
  56.   for (i=0; i<numProps; i++) {  
  57.   }  
  58.   free(props);  
  59.    
  60.    
  61.   // Copy Protocol List  
  62.   unsigned int numProtocols = 9999999;  
  63.   Protocol **ps = class_copyProtocolList(NSMA, &numProtocols);  
  64.   printf("\nNum Protocols: %d\n", numProtocols);  
  65.   for (i=0; i<numProtocols; i++) {  
  66.     Protocol *p = ps[i];  
  67.     printf("  %s\n", protocol_getName(p));  
  68.   }  
  69.   free(ps);  
  70.    
  71.   // Copy Method List (instance methods)  
  72.   unsigned int numMethods = 99999999;  
  73.   Method * methods = NULL;  
  74.   methods = class_copyMethodList(NSMA, &numMethods);  
  75.   printf("\nNum Instance Methods: %d\n", numMethods);  
  76.   for (i=0; i<numMethods; i++) {  
  77.     Method m = methods[i];  
  78.     SEL name = method_getName(m);  
  79.     printf("  %s (%s)\n", sel_getName(name), method_getTypeEncoding(m));  
  80.   }  
  81.   free(methods);  
  82.    
  83.    
  84.   // Copy Method List (class methods)  
  85.   numMethods = 0;  
  86.   methods = NULL;  
  87.   methods = class_copyMethodList(metaclass, &numMethods);  
  88.   printf("\nNum Class Methods: %d\n", numMethods);  
  89.   for (i=0; i<numMethods; i++) {  
  90.     Method m = methods[i];  
  91.     SEL name = method_getName(m);  
  92.     printf("  %s (%s)\n", sel_getName(name), method_getTypeEncoding(m));  
  93.   }  
  94.   free(methods);  
  95.    
  96.   // get superclass  
  97.   Class superclass = NSMA;  
  98.   printf("\nWalking inheritance chain:\n");  
  99.   printf("  %s\n", name);  
  100.   unsigned int numClasses = 0;  
  101.   do {  
  102.     printf("  ");  
  103.     numClasses++;  
  104.     superclass = class_getSuperclass(superclass);  
  105.     for (i=0; i<numClasses; i++) {  
  106.       printf("  ");  
  107.     }  
  108.     printf("↳ %s\n", class_getName(superclass));  
  109.   } while(superclass);  
  110.    
  111.    
  112.   return 0;  
  113. }</span>  


<div class="line" id="file-class_introspection-m-LC1">
</div>

4、打印所有协议

[cpp]  view plain copy print ?
  1. #include <objc/runtime.h>  
  2. #include <dlfcn.h>  
  3. #include <stdio.h>  
  4.    
  5. int main(int argc, char **argv) {  
  6.    
  7.   dlopen("/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation", RTLD_LAZY);  
  8.    
  9.   unsigned int i, n;  
  10.   Protocol **ps = NULL;  
  11.    
  12.   ps = objc_copyProtocolList(&n);  
  13.   printf("Num Protocols: %d\n", n);  
  14.    
  15.    
  16.   // Do stuff with `classes`  
  17.   for (i=0; i<n; i++) {  
  18.     Protocol *p = ps[i];  
  19.     printf("%s\n", protocol_getName(p));  
  20.   }  
  21.    
  22.   free(ps);  
  23.    
  24.   return 0;  
  25. }  

5、获取某个framework的所有class

[cpp]  view plain copy print ?
  1. #import <Foundation/Foundation.h>  
  2. #include <objc/runtime.h>  
  3. #include <dlfcn.h>  
  4. #include <stdio.h>  
  5.    
  6. int main(int argc, char **argv) {  
  7.    
  8.   dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", RTLD_LAZY);  
  9.   dlopen("/System/Library/Frameworks/AppKit.framework/AppKit", RTLD_LAZY);  
  10.    
  11.   int i = 0;  
  12.   int numClasses;  
  13.   Class * classes = NULL;  
  14.    
  15.   numClasses = objc_getClassList(NULL, 0);  
  16.   printf("Num classes: %d\n", numClasses);  
  17.    
  18.   if (numClasses > 0 ) {  
  19.     classes = malloc(sizeof(Class) * numClasses);  
  20.     numClasses = objc_getClassList(classes, numClasses);  
  21.    
  22.     // Do stuff with `classes`  
  23.     for (i=0; i<numClasses; i++) {  
  24.       Class c = classes[i];  
  25.       printf("%s\n", class_getName(c));  
  26.     }  
  27.    
  28.     free(classes);  
  29.   }  
  30.    
  31.   return 0;  
  32. }  

6、HelloWorld


<div class="line" id="file-hello-m-LC3"><div class="dp-highlighter bg_cpp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 824.65625px; overflow: auto; padding-top: 1px; opacity: 0.6; margin: 18px 0px !important; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/cpp.gif); background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: 100% 0%; background-repeat: no-repeat;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108);"><strong>[cpp]</strong> <a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/lizhongfu2013/article/details/9665579#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><div style="position: absolute; left: 520px; top: 7711px; width: 29px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-cpp" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <objc/runtime.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <dlfcn.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="preprocessor" style="margin: 0px; padding: 0px; border: none; color: gray; background-color: inherit;">#include <stdio.h></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> main() {  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *sdl_library = dlopen(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, RTLD_LAZY);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> (sdl_library == NULL) {  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"Got dlopen error!\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  } <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">else</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> {  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"dlopen successful!\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Set up an NSAutoreleasePool</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Class nsautoreleasepool = objc_getClass(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"NSAutoreleasePool"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id pool = class_createInstance(nsautoreleasepool, 0);  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    SEL initSel = sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"init"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id poolAfterInit = objc_msgSend(pool, initSel);  </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Set up a NSString with the contents "Hello World" from a C string</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    Class nsstring = objc_getClass(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"NSString"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    SEL stringUTF8sel = sel_registerName(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"stringWithUTF8String:"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    id hello = objc_msgSend(nsstring, stringUTF8sel, <span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"Hello World\n"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// Print it back out as a C string</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">    printf(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"%s"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, (</span><span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">char</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *)objc_msgSend(hello, sel_registerName(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"UTF8String"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)));  </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  }  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">   </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> 0;  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li></ol></div>

</div>

7、获取size

[cpp]  view plain copy print ?
  1. <span style="font-size:12px;">#include <objc/runtime.h>  
  2. #include <dlfcn.h>  
  3. #include <stdio.h>  
  4.    
  5. int main(int argc, char **argv) {  
  6.    
  7.   dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", RTLD_LAZY);  
  8.   dlopen("/System/Library/Frameworks/AppKit.framework/AppKit", RTLD_LAZY);  
  9.   dlopen("/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY);  
  10.    
  11.   char * className = argc == 2 ? argv[1] : "NSMutableArray";  
  12.   Class NSMA = objc_getClass(className);  
  13.    
  14.   if (!NSMA) {  
  15.     printf("Could not load Class: %s\n", className);  
  16.     return 1;  
  17.   }  
  18.    
  19.   const char *name = class_getName(NSMA);  
  20.   printf("class_getName: %s\n", name);  
  21.    
  22.   int version = class_getVersion(NSMA);  
  23.   printf("class_getVersion: %d\n", version);  
  24.    
  25.   const char *weakIvarLayout = class_getWeakIvarLayout(NSMA);  
  26.   printf("class_getWeakIvarLayout: %s\n", weakIvarLayout);  
  27.    
  28.   BOOL isMetaClass = class_isMetaClass(NSMA);  
  29.   printf("class_isMetaClass: %s\n", isMetaClass ? "yes" : "no");  
  30.    
  31.   // get the metaclass  
  32.   id metaclass = NSMA->isa;  
  33.   isMetaClass = class_isMetaClass(metaclass);  
  34.   printf("class_isMetaClass(isa): %s\n", isMetaClass ? "yes" : "no");  
  35.    
  36.    
  37.   // Look up the Ivars  
  38.   unsigned int i = 0;  
  39.   unsigned int numIvars = 999999;  
  40.   Ivar *ivars = NULL;  
  41.   ivars = class_copyIvarList(NSMA, &numIvars);  
  42.   printf("\nNum ivars: %d\n", numIvars);  
  43.   for (; i<numIvars; i++) {  
  44.     Ivar ivar = ivars[i];  
  45.     const char *ivarName = ivar_getName(ivar);  
  46.     printf("  %s (%s)\n", ivarName, ivar_getTypeEncoding(ivar));  
  47.   }  
  48.   free(ivars);  
  49.    
  50.    
  51.   // Copy the Property List  
  52.   unsigned int numProps = 999999;  
  53.   objc_property_t *props = NULL;  
  54.   props = class_copyPropertyList(NSMA, &numProps);  
  55.   printf("\nNum props: %d\n", numProps);  
  56.   for (i=0; i<numProps; i++) {  
  57.   }  
  58.   free(props);  
  59.    
  60.    
  61.   // Copy Protocol List  
  62.   unsigned int numProtocols = 9999999;  
  63.   Protocol **ps = class_copyProtocolList(NSMA, &numProtocols);  
  64.   printf("\nNum Protocols: %d\n", numProtocols);  
  65.   for (i=0; i<numProtocols; i++) {  
  66.     Protocol *p = ps[i];  
  67.     printf("  %s\n", protocol_getName(p));  
  68.   }  
  69.   free(ps);  
  70.    
  71.   // Copy Method List (instance methods)  
  72.   unsigned int numMethods = 99999999;  
  73.   Method * methods = NULL;  
  74.   methods = class_copyMethodList(NSMA, &numMethods);  
  75.   printf("\nNum Instance Methods: %d\n", numMethods);  
  76.   for (i=0; i<numMethods; i++) {  
  77.     Method m = methods[i];  
  78.     SEL name = method_getName(m);  
  79.     printf("  %s (%s)\n", sel_getName(name), method_getTypeEncoding(m));  
  80.   }  
  81.   free(methods);  
  82.    
  83.    
  84.   // Copy Method List (class methods)  
  85.   numMethods = 0;  
  86.   methods = NULL;  
  87.   methods = class_copyMethodList(metaclass, &numMethods);  
  88.   printf("\nNum Class Methods: %d\n", numMethods);  
  89.   for (i=0; i<numMethods; i++) {  
  90.     Method m = methods[i];  
  91.     SEL name = method_getName(m);  
  92.     printf("  %s (%s)\n", sel_getName(name), method_getTypeEncoding(m));  
  93.   }  
  94.   free(methods);  
  95.    
  96.   // get superclass  
  97.   Class superclass = NSMA;  
  98.   printf("\nWalking inheritance chain:\n");  
  99.   printf("  %s\n", name);  
  100.   unsigned int numClasses = 0;  
  101.   do {  
  102.     printf("  ");  
  103.     numClasses++;  
  104.     superclass = class_getSuperclass(superclass);  
  105.     for (i=0; i<numClasses; i++) {  
  106.       printf("  ");  
  107.     }  
  108.     printf("↳ %s\n", class_getName(superclass));  
  109.   } while(superclass);  
  110.    
  111.    
  112.   return 0;  
  113. }</span>  

[cpp]  view plain copy print ?
  1. #import <Foundation/Foundation.h>  
  2. #include <objc/runtime.h>  
  3. #include <dlfcn.h>  
  4. #include <stdio.h>  
  5.   
  6. int main() {  
  7.       
  8.     void *sdl_library = dlopen("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation", RTLD_LAZY);  
  9.     if (sdl_library == NULL) {  
  10.         printf("Got dlopen error!\n");  
  11.     } else {  
  12.         printf("dlopen successful!\n");  
  13.     }  
  14.       
  15.     printf("sizeof(Class): %lu\n"sizeof(Class));  
  16.     printf("sizeof(Method): %lu\n"sizeof(Method));  
  17.     printf("sizeof(unsigned long int): %lu\n"sizeof(unsigned long int));  
  18.     printf("sizeof(struct int, void*): %lu\n"sizeof(struct test { int test; void * ptr; }));  
  19.     printf("sizeof(struct int, int, void*): %lu\n"sizeof(struct test2 { int test; int test2; void * ptr; }));  
  20.     printf("sizeof(struct void*, int, int, void*): %lu\n"sizeof(struct test3 { void *ptr2; int test2; void * ptr; }));  
  21.       
  22.     return 0;  
  23. }  


8、捕获异常

[cpp]  view plain copy print ?
  1. #include <objc/runtime.h>  
  2. #include <objc/objc-exception.h>  
  3. #include <dlfcn.h>  
  4. #include <stdio.h>  
  5.    
  6. void uncaughtExceptionHandler (id exception) {  
  7.   fprintf(stderr, "Inside uncaughtException handler!!!!\n");  
  8.   fprintf(stderr, "Exception Type: %s\n", (const char *)objc_msgSend(objc_msgSend(exception, sel_registerName("name")), sel_registerName("UTF8String")));  
  9.   fprintf(stderr, "Exception Reason: %s\n", (const char *)objc_msgSend(objc_msgSend(exception, sel_registerName("reason")), sel_registerName("UTF8String")));  
  10. }  
  11.    
  12.    
  13. int main() {  
  14.    
  15.   void *sdl_library = dlopen("/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation", RTLD_LAZY);  
  16.   if (sdl_library == NULL) {  
  17.     printf("Got dlopen error!\n");  
  18.   } else {  
  19.     printf("dlopen successful!\n");  
  20.    
  21.     // Set up an NSAutoreleasePool  
  22.     Class nsautoreleasepool = objc_getClass("NSAutoreleasePool");  
  23.     id pool = class_createInstance(nsautoreleasepool, 0);  
  24.     SEL initSel = sel_registerName("init");  
  25.     id poolAfterInit = objc_msgSend(pool, initSel);  
  26.    
  27.    
  28.     // Set up an uncaughtException handler  
  29.     objc_setUncaughtExceptionHandler((objc_uncaught_exception_handler)uncaughtExceptionHandler);  
  30.    
  31.    
  32.     // Intentionally throw an exception, by sending a bad selector to an object  
  33.     Class nsobject = objc_getClass("NSObject");  
  34.     SEL badSel = sel_registerName("badSelector");  
  35.     objc_msgSend(nsobject, badSel);  
  36.   }  
  37.    
  38.   return 0;  
  39. }  


9、设备当前运行的进程

[cpp]  view plain copy print ?
  1. - (NSArray *)runningProcesses {  
  2.       
  3.     int mib[4] = {CTL_KERN,KERN_PROC, KERN_PROC_ALL, 0};  
  4.     size_t miblen = 4;  
  5.       
  6.     size_t size;  
  7.     int st = sysctl(mib, miblen,NULL, &size, NULL, 0);  
  8.       
  9.     struct kinfo_proc * process =NULL;  
  10.     struct kinfo_proc * newprocess =NULL;  
  11.       
  12.     do {  
  13.           
  14.         size += size / 10;  
  15.         newprocess = realloc(process, size);  
  16.           
  17.         if (!newprocess){  
  18.               
  19.             if (process){  
  20.                 free(process);  
  21.             }  
  22.               
  23.             return nil;  
  24.         }  
  25.           
  26.         process = newprocess;  
  27.         st = sysctl(mib, miblen, process, &size,NULL, 0);  
  28.           
  29.     } while (st == -1 &&errno == ENOMEM);  
  30.       
  31.     if (st == 0){  
  32.           
  33.         if (size % sizeof(structkinfo_proc) == 0){  
  34.             int nprocess = size / sizeof(struct kinfo_proc);  
  35.               
  36.             if (nprocess){  
  37.                   
  38.                 NSMutableArray * array = [[NSMutableArrayalloc] init];  
  39.                   
  40.                 for (int i = nprocess -1; i >= 0; i--){  
  41.                       
  42.                     NSString * processID = [[NSStringalloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];  
  43.                     NSString * processName = [[NSStringalloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];  
  44.                       
  45.                     NSDictionary * dict = [[NSDictionaryalloc] initWithObjects:[NSArrayarrayWithObjects:processID, processName, nil]  
  46.                                                                        forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName",nil]];  
  47.                     [processID release];  
  48.                     [processName release];  
  49.                     [array addObject:dict];  
  50.                     [dict release];  
  51.                 }  
  52.                   
  53.                 free(process);  
  54.                 return [array autorelease];  
  55.             }  
  56.         }  
  57.     }  
  58.       
  59.     return nil;  
  60. }  
  61. // 当前运行的进程  
  62.     NSArray * processes = [selfrunningProcesses];  
  63.     for (NSDictionary * dictin processes){  
  64.         NSLog(@"%@ - %@", [dictobjectForKey:@"ProcessID"], [dictobjectForKey:@"ProcessName"]);  
  65.     }  

你可能感兴趣的:(ios,c&c++)