{"id":135,"date":"2015-04-10T18:06:05","date_gmt":"2015-04-10T10:06:05","guid":{"rendered":"http:\/\/www.yarpee.com\/?p=135"},"modified":"2015-04-10T20:12:40","modified_gmt":"2015-04-10T12:12:40","slug":"objective-c","status":"publish","type":"post","link":"http:\/\/www.yarpee.cn\/?p=135","title":{"rendered":"Objective-C"},"content":{"rendered":"<p><a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Conceptual\/ProgrammingWithObjectiveC\/Introduction\/Introduction.html\" target=\"_blank\">https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Conceptual\/ProgrammingWithObjectiveC\/Introduction\/Introduction.html<\/a><\/p>\n<p>Protocol\u7684\u5b9a\u4e49\u5f62\u5f0f\uff1a<br \/>\n@protocol ProtocolName<br \/>\n\/\/ list of methods and properties<br \/>\n@end<\/p>\n<p>\u901a\u8fc7\u5c5e\u6027\u5f15\u7528\u4e00\u4e2a\u5b9e\u73b0\u4e86Protocol\u7684\u5bf9\u8c61\uff1a<br \/>\n@property (weak) id &lt;XYZPieChartViewDataSource&gt; dataSource;<\/p>\n<p>Protocol\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u5fc5\u987b\u5b9e\u73b0\u7684\u65b9\u6cd5\u548c\u53ef\u9009\u5b9e\u73b0\u7684\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u662f\u5fc5\u987b\u5b9e\u73b0\u7684\u3002\u4f7f\u7528\u5173\u952e\u5b57@optional\u548c@required\u53ef\u4ee5\u58f0\u660e\u5b83\u4eec\u4ee5\u4e0b\u7684\u65b9\u6cd5\u662f\u53ef\u9009\u6216\u5fc5\u987b\u5b9e\u73b0\u7684\uff1a<br \/>\n@protocol XYZPieChartViewDataSource<br \/>\n&#8211; (NSUInteger)numberOfSegments;<br \/>\n&#8211; (CGFloat)sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;<br \/>\n@optional<br \/>\n&#8211; (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex;<br \/>\n&#8211; (BOOL)shouldExplodeSegmentAtIndex:(NSUInteger)segmentIndex;<br \/>\n@required<br \/>\n&#8211; (UIColor *)colorForSegmentAtIndex:(NSUInteger)segmentIndex;<br \/>\n@end<\/p>\n<p>\u8c03\u7528Protocol\u4e2d\u7684\u53ef\u9009\u65b9\u6cd5\u65f6\u9700\u8981\u901a\u8fc7respondsToSelector:\u548c@selector()\u5148\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u5b9e\u73b0\u4e86\u8be5\u65b9\u6cd5\uff1a<br \/>\nNSString *thisSegmentTitle;<br \/>\nif ([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]) {<br \/>\nthisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];<br \/>\n}<\/p>\n<p>Protocol\u53ef\u4ee5\u7ee7\u627f\uff1a<br \/>\n@protocol MyProtocol &lt;NSObject&gt;<br \/>\n&#8230;<br \/>\n@end<\/p>\n<p>\u7c7b\u5b9e\u73b0\u591a\u4e2a\u534f\u8bae\uff1a<br \/>\n@interface MyClass : NSObject &lt;MyProtocol, AnotherProtocol, YetAnotherProtocol&gt;<br \/>\n&#8230;<br \/>\n@end<br \/>\n\u6ce8\u610f\uff1a\u7f16\u8bd1\u5668\u4e0d\u4f1a\u5408\u6210\u534f\u8bae\u4e2d\u58f0\u660e\u7684\u5c5e\u6027\u3002<\/p>\n<p>Block\u4e5f\u662fOC\u5bf9\u8c61\uff0c\u5176\u5b9a\u4e49\u5f62\u5f0f\u4e3a\uff1a<br \/>\n^{<br \/>\nNSLog(@&#8221;This is a block&#8221;);<br \/>\n}<br \/>\n^ double (double firstValue, double secondValue) {<br \/>\nreturn firstValue * secondValue;<br \/>\n}<\/p>\n<p>\u540cC\u8bed\u8a00\u91cc\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u6307\u5411\u4e00\u4e2a\u51fd\u6570\u4e00\u6837\uff0cOC\u91cc\u53ef\u4ee5\u4f7f\u7528Block Pointer\uff1a<br \/>\nvoid (^simpleBlock)(void);<\/p>\n<p>Block\u80fd\u6293\u4f4f\u95ed\u5305\u8303\u56f4\u5185\u7684\u503c\uff0c\u4f46\u5b83\u4e0d\u80fd\u6539\u53d8\u503c\u3002\u5982\u679cBlock\u8981\u6539\u53d8\u4e00\u4e2a\u88abBlock\u6293\u4f4f\u7684\u53d8\u91cf\u7684\u503c\uff0c\u9700\u8981\u5bf9\u8be5\u53d8\u91cf\u4f7f\u7528__block\u4fee\u9970\u7b26\u3002<br \/>\n\u6ce8\u610f\uff1aBlock\u82e5\u6293\u4f4f\u4e86self\u65f6\uff0c\u53ef\u80fd\u5f15\u8d77\u5f3a\u5f15\u7528\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\u3002<br \/>\n\u89e3\u51b3\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\u53ef\u4ee5\u5728Block\u5185\u90e8\u4f7f\u7528__weak\u7684self\uff1a<br \/>\n&#8211; (void)configureBlock {<br \/>\nXYZBlockKeeper * __weak weakSelf = self;<br \/>\nself.block = ^{<br \/>\n[weakSelf doSomething]; \/\/ capture the weak reference<br \/>\n\/\/ to avoid the reference cycle<br \/>\n}<br \/>\n}<\/p>\n<p>Class\u4e2d\u4f7f\u7528\u5c5e\u6027\u5f15\u7528Block\u65f6\u8981\u4f7f\u7528copy\u5c5e\u6027\u4fee\u9970\u7b26\uff1a<br \/>\n@interface XYZObject : NSObject<br \/>\n@property (copy) void (^blockProperty)(void);<br \/>\n@end<\/p>\n<p>Operation Queues\u662fCocoa\u8fdb\u884c\u4efb\u52a1\u8c03\u5ea6\u7684\u4e00\u4e2a\u9014\u5f84\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aNSOperation\u5b9e\u4f8b\u5c06\u4e00\u4e2a\u4efb\u52a1\u53ca\u5176\u9700\u8981\u7684\u6570\u636e\u5c01\u88c5\u8d77\u6765\uff0c\u7136\u540e\u5c06\u5b83\u52a0\u5165\u4e00\u4e2aNSOperationQueue\u6765\u6267\u884c\u3002<br \/>\n\u4f7f\u7528NSBlockOperation\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528Block\u7684Operation\uff1a<br \/>\nNSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{<br \/>\n&#8230;<br \/>\n}];<\/p>\n<p>\/\/ schedule task on main queue:<br \/>\nNSOperationQueue *mainQueue = [NSOperationQueue mainQueue];<br \/>\n[mainQueue addOperation:operation];<\/p>\n<p>\/\/ schedule task on background queue:<br \/>\nNSOperationQueue *queue = [[NSOperationQueue alloc] init];<br \/>\n[queue addOperation:operation];<\/p>\n<p>Block\u4e5f\u53ef\u4ee5\u4f7f\u7528Grand Central Dispatch (GCD)\u63a7\u5236\u7684Dispatch Queues\u6765\u6267\u884c\u3002<br \/>\ndispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);<\/p>\n<p>dispatch_async(queue, ^{<br \/>\nNSLog(@&#8221;Block for asynchronous execution&#8221;);<br \/>\n});<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/developer.apple.com\/library\/mac\/documentation\/C &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.yarpee.cn\/?p=135\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cObjective-C\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-mac_skill"],"_links":{"self":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/135"}],"collection":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=135"}],"version-history":[{"count":2,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":137,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/135\/revisions\/137"}],"wp:attachment":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}