{"id":1964,"date":"2017-01-04T19:27:22","date_gmt":"2017-01-04T11:27:22","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=1964"},"modified":"2017-08-24T16:02:21","modified_gmt":"2017-08-24T08:02:21","slug":"iosobjective-c%e5%b8%b8%e7%94%a8api","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=1964","title":{"rendered":"[iOS]Objective-C\u5e38\u7528\u7684Cocoa Touch API"},"content":{"rendered":"<h1>dispatch_async<\/h1>\n<p>[c]dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{<br \/>\n    \/\/ \u8017\u65f6\u7684\u64cd\u4f5c<br \/>\n    dispatch_async(dispatch_get_main_queue(), ^{<br \/>\n        \/\/ \u66f4\u65b0\u754c\u9762<br \/>\n    });<br \/>\n}); [\/c]<\/p>\n<p><!--more--><\/p>\n<p>example:<\/p>\n<p>[c]dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{<br \/>\n    NSURL * url = [NSURL URLWithString:@&quot;http:\/\/learn-house.idv.tw\/xxx.jpg&quot;];<br \/>\n    NSData * data = [[NSData alloc]initWithContentsOfURL:url];<br \/>\n    UIImage *image = [[UIImage alloc]initWithData:data];<br \/>\n    if (data != nil) {<br \/>\n        dispatch_async(dispatch_get_main_queue(), ^{<br \/>\n            self.imageView.image = image;<br \/>\n         });<br \/>\n    }<br \/>\n});[\/c]<\/p>\n<pre>serial \u7684 queue<\/pre>\n<p>[c]dispatch_async(dispatch_queue_create(&quot;com.learnhouse.queue&quot;, DISPATCH_QUEUE_SERIAL), ^{<br \/>\n \/\/your code<br \/>\n});[\/c]<\/p>\n<h1>NSDictionary<\/h1>\n<p>[c]\/\/\u4e00\u500bkey &#8211; value<br \/>\nNSDictionary *dic1 = [NSDictionary dictionaryWithObject:@&quot;@123&quot; forKey:@&quot;key1&quot;];<br \/>\n\/\/\u5169\u500b\u4ee5\u4e0a key &#8211; value<br \/>\nNSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@&quot;value1&quot;,@&quot;key1&quot;,@&quot;value2&quot;,@&quot;key2&quot; ,nil];<br \/>\n\/\/\u7528\u5b57\u5178\u70ba\u53e6\u4e00\u500b\u5b50\u5b57\u5178\u521d\u59cb\u5316<br \/>\nNSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic1]<br \/>\n\/\/\u65b0\u65b9\u6cd5\u8ce6\u503c<br \/>\nNSDictionary *dic4 = @{@&quot;key1&quot;: @&quot;value1&quot;,@&quot;key2&quot;: @&quot;value2&quot;};<br \/>\n\/\/\u4ee5\u70ba\u4ef6\u5167\u5bb9\u521d\u59cb\u5316\u4e00\u500b\u5b57\u5178<br \/>\nNSDictionary *dic5 = [NSDictionary dictionaryWithContentsOfFile:path];<br \/>\n\/\/\u5c07\u5b57\u5178\u7684key\u8f49\u6210\u4e00\u500b\u679a\u8209\u5c0d\u8c61\uff0c\u7528\u65bc\u904d\u6b77<br \/>\nNSEnumerator *enumerator = [dic4 keyEnumerator];<br \/>\n\/\/\u5c07\u5b57\u5178\u7684value\u8f49\u6210\u4e00\u500b\u679a\u8209\u5c0d\u8c61\uff0c\u7528\u65bc\u904d\u6b77<br \/>\nNSEnumerator *enumerator = [dic6 objectEnumerator];[\/c]<\/p>\n<p>\u5b57\u5178\u7684\u5e38\u7528\u65b9\u6cd5<\/p>\n<p>[c]\/\/\u8fd4\u56de\u5b57\u5178\u9375\u503c\u5c0d\u7684\u500b\u6578<br \/>\nNSInteger count = [dic4 count];<br \/>\n\/\/\u901a\u904ekey\u7372\u53d6\u5c0d\u61c9\u7684value\u5c0d\u8c61<br \/>\nNSObject *object = [dic2 objectForKey:@&quot;key1&quot;];<br \/>\n\/\/\u7372\u53d6\u6240\u6709\u9375\u7684\u96c6\u5408<br \/>\nNSArray *keys = [dic3 allKeys];<br \/>\n\/\/\u7372\u53d6\u6240\u6709\u503c\u7684\u96c6\u5408<br \/>\nNSArray *values = [dic3 allValues];[\/c]<\/p>\n<h1>NSMutableDictionary<\/h1>\n<p>\u53ef\u8b8a\u5b57\u5178\u8207\u4e0d\u53ef\u8b8a\u5b57\u5178\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u76f8\u540c\uff0c\u4f46\u4e0d\u80fd\u7528\u65b0\u65b9\u6cd5\u3002\u4e0b\u9762\u53ea\u4ecb\u7d39\u5e38\u7528\u65b9\u6cd5<\/p>\n<p>\u5e38\u7528\u65b9\u6cd5<\/p>\n<p>[c]NSMutableDictionary *dic6 = [NSMutableDictionary dictionary];<br \/>\n\/\/\u50cf\u5b57\u5178\u4e2d\u8ffd\u52a0\u4e00\u500b\u65b0\u7684 key5 \u548c value5<br \/>\n[dic6 setObject:@&quot;value5&quot; forKey:@&quot;key5&quot;];<br \/>\n\/\/\u50cf\u5b57\u5178\u4e2d\u6dfb\u52a0\u6574\u500b\u5b57\u5178\u5c0d\u8c61<br \/>\n[dic6 addEntriesFromDictionary:dic1];<br \/>\n\/\/\u5c07\u5b57\u51786\u7684\u5c0d\u8c61\u5167\u5bb9\u8a2d\u7f6e\u8207\u5b57\u51781\u7684\u5c0d\u8c61\u5167\u5bb9\u76f8\u540c<br \/>\n[dic6 setDictionary:dic1];<br \/>\n\/\/\u522a\u9664\u9375\u6240\u5c0d\u61c9\u7684\u9375\u503c\u5c0d<br \/>\n[dic6 removeObjectForKey:@&quot;key1&quot;];<br \/>\n\/\/\u522a\u9664\u6578\u7d44\u4e2d\u7684\u6240\u6709key \u5c0d\u61c9\u7684\u9375\u503c\u5c0d<br \/>\nNSArray *array = @[@&quot;key1&quot;,@&quot;key2&quot;,@&quot;key3&quot;];<br \/>\n[dic6 removeObjectsForKeys:array];<br \/>\n\/\/\u79fb\u9664\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5c0d\u8c61<br \/>\n[dic6 removeAllObjects];[\/c]<\/p>\n<p>\u4e00\u822c\u904d\u6b77<\/p>\n<p>[c]for (int i = 0; i &lt; [dic3 count]; i++) {<br \/>\n    id key = [keys objectAtIndex:i];<br \/>\n    id obj = [dic3 objectForKey:key];<br \/>\n    NSLog(@&quot;%@&quot;,obj);<br \/>\n}[\/c]<\/p>\n<p>\u5feb\u901f\u904d\u6b77<\/p>\n<p>[c]for (id key in dic3) {<br \/>\n    id obj = [dic3 objectForKey:key];<br \/>\n    NSLog(@&quot;%@&quot;,obj);<br \/>\n}[\/c]<\/p>\n<p>\u679a\u8209\u7684\u8fa6\u6cd5\u904d\u6b77<\/p>\n<p>[c]NSEnumerator *enumerator = [dic4 keyEnumerator];<br \/>\nid key = [enumerator nextObject];<br \/>\nwhile (key) {<br \/>\n    id obj = [dic4 objectForKey:key];<br \/>\n    NSLog(@&quot;%@&quot;,obj);<br \/>\n    key = [enumerator nextObject];<br \/>\n}[\/c]<\/p>\n<p>\u5beb\u5165BOOL\u503c<\/p>\n<p>[c][dicTemp setObject:[NSNumber numberWithBool:YES] forKey:@&quot;is_default&quot;];<br \/>\n\/\/or<br \/>\ndicTemp[@&quot;is_default&quot;] = @YES;[\/c]<\/p>\n<h1>NSString<\/h1>\n<p>\u5207\u5b57\u4e32\u7684\u65b9\u6cd5<br \/>\n(NSString *)substringFromIndex:(NSUInteger)anIndex<br \/>\n(NSString *)substringToIndex:(NSUInteger)anIndex<br \/>\n(NSString *)substringWithRange:(NSRange)aRange<\/p>\n<p>example:<\/p>\n<p>[c]NSString *str = @&quot;1234567890&quot;;<br \/>\nNSLog(@&quot;Original String: %@&quot;, str);<br \/>\nNSLog(@&quot;substringFromIndex: 5 ==&gt; %@&quot;, [str substringFromIndex:5]);<br \/>\nNSLog(@&quot;substringToIndex: 5 ==&gt; %@&quot;, [str substringToIndex:5]);<br \/>\nNSLog(@&quot;substringWithRange: from 7, length 3 ==&gt; %@&quot;, [str substringWithRange:NSMakeRange(7, 3)]);[\/c]<\/p>\n<p>result:<\/p>\n<p>[c]Original String: 1234567890<br \/>\nsubstringFromIndex: 5 ==&gt; 67890<br \/>\nsubstringToIndex: 5 ==&gt; 12345<br \/>\nsubstringWithRange: from 7, length 3 ==&gt; 890[\/c]<\/p>\n<p>\u5b57\u4e32\u53d6\u4ee3<br \/>\n&#8211; (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target<br \/>\nwithString:(NSString *)replacement<\/p>\n<p>example:<\/p>\n<p>[c]NSString *str = @&quot;This is a string&quot;;<br \/>\nstr = [str stringByReplacingOccurrencesOfString:@&quot;string&quot;<br \/>\n                                     withString:@&quot;duck&quot;];<br \/>\n\/\/This is a duck<br \/>\n[\/c]<\/p>\n<p>\u524d\u6233\u5b57\u4e32\u6bd4\u5c0d<\/p>\n<p>[c]if ([myString hasPrefix:@&quot;http&quot;]) {<br \/>\n}[\/c]<\/p>\n<h1>NSString \u548c NSDate \u4e92\u76f8\u8f49\u63db<\/h1>\n<p>\u7531 NSDate \u8f49\u63db\u70ba NSString:<\/p>\n<p>[c]NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];<br \/>\n[dateFormatter setDateFormat:@&quot;yyyy-MM-dd HH:mm:ss&quot;];<br \/>\nNSString *strDate = [dateFormatter stringFromDate:[NSDate date]];<br \/>\nNSLog(@&quot;%@&quot;, strDate);[\/c]<\/p>\n<p>\u7531 NSString \u8f49\u63db\u70ba NSDate:<\/p>\n<p>[c]NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];<br \/>\n[dateFormatter setDateFormat:@&quot;yyyy-MM-dd HH:mm:ss&quot;];<br \/>\nNSDate *date = [dateFormatter dateFromString:@&quot;2010-08-04 16:01:03&quot;];<br \/>\nNSLog(@&quot;%@&quot;, date);[\/c]<\/p>\n<h1>char \u8f49\u63db\u6210 NSString<\/h1>\n<p>char myChar = &#8216;r&#8217;;<br \/>\n[NSString stringWithFormat:@&#8221;%c&#8221; , myChar];<\/p>\n<h1>NSString \u8f49\u63db\u6210 char<\/h1>\n<p>const char *myChar = [myString UTF8String];<\/p>\n<h1>\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u8ddf\u6700\u5c0f\u503c<\/h1>\n<p>[c]NSArray * arr = [NSArray arrayWithObjects:@&quot;1&quot;,@&quot;10&quot;,@&quot;5&quot;, @&quot;31&quot;, nil];<br \/>\n\/\/\u5f97\u5230\u6570\u7ec4\u4e2d\u6700\u5c0f\u7684\u503c<br \/>\nNSInteger min = [[arr valueForKeyPath:@&quot;@min.intValue&quot;] integerValue];<br \/>\n\/\/\u5f97\u5230\u6570\u7ec4\u4e2d\u6700\u5927\u7684\u503c<br \/>\nNSInteger max = [[arr valueForKeyPath:@&quot;@max.intValue&quot;] integerValue];<br \/>\nNSLog(@&quot;min = %ld  max = %ld&quot;, (long)min, (long)max);[\/c]<\/p>\n<h1>Split an NSString<\/h1>\n<p>[c]NSArray* foo = [@&quot;10\/04\/2011&quot; componentsSeparatedByString: @&quot;\/&quot;];<br \/>\nNSString* firstBit = [foo objectAtIndex: 0];[\/c]<\/p>\n<h1>UITextField<\/h1>\n<p><a href=\"http:\/\/learn-house.idv.tw\/?p=1990\" target=\"_blank\" rel=\"noopener\">http:\/\/learn-house.idv.tw\/?p=1990<\/a><\/p>\n<h1>\u5075\u6e2cview\u7684\u8fd4\u56deback button<\/h1>\n<p>[c]-(void) viewWillDisappear:(BOOL)animated {<br \/>\n    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {<br \/>\n        \/\/ Navigation button was pressed. Do some stuff<br \/>\n        [self.navigationController popViewControllerAnimated:NO];<br \/>\n    }<br \/>\n    [super viewWillDisappear:animated];<br \/>\n}[\/c]<\/p>\n<h1>Operation Queues\u7684\u4f7f\u7528<\/h1>\n<p>[c]NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];  \/\/main queue<br \/>\nNSOperationQueue *queue = [[NSOperationQueue alloc] init]; \/\/other queue<br \/>\nqueue.maxConcurrentOperationCount = 1;\/\/1 is mean serial, others are mean concurrent<br \/>\nNSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{<br \/>\n                \/\/\u4efb\u52d9\u57f7\u884c<br \/>\n}];<br \/>\n[queue addOperation:operation];[\/c]<\/p>\n<p>or<\/p>\n<p>[c]NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];<br \/>\n[oprationQueue addOperationWithBlock:^{<br \/>\n    \/\/your code<br \/>\n}];[\/c]<\/p>\n<p>NSInvocationOperation<\/p>\n<p>[c]NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];<br \/>\noprationQueue.maxConcurrentOperationCount = 2;\/\/\u8a2d\u7f6e\u6700\u5927\u4e26\u767c\u6578\u6578<\/p>\n<p>\/\/NSInvocationOperation \u70baNSOperation\u7684\u5177\u9ad4\u5b50\u985e\uff0c\u662f\u4e00\u500b\u975e\u4e26\u767c\u64cd\u4f5c\u3002<br \/>\nNSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@&quot;2.1&quot;];<br \/>\n[oprationQueue addOperation:invation];\/\/\u5c07\u4efb\u52d9\u6dfb\u52a0\u5230\u5217\u968a\u4e2d<\/p>\n<p>\/\/\u7b2c\u4e8c\u500b\u4efb\u52d9<br \/>\nNSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(action:) object:@&quot;2.2&quot;];<br \/>\ninvation2.queuePriority = NSOperationQueuePriorityVeryHigh;\/\/\u8a2d\u7f6einvation2\u7dda\u7a0b\u512a\u5148\u7d1a<br \/>\n[oprationQueue addOperation:invation2];[\/c]<\/p>\n<h1>NSThread &amp; NSTimer<\/h1>\n<p>[c]<br \/>\n\/\/NSThread<br \/>\n[NSThread sleepForTimeInterval:5];<\/p>\n<p>\/\/NSTimer<br \/>\n[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(yourmethod:) userInfo:nil repeats:NO];<\/p>\n<p>\/\/\u50b3\u905euserInfo<br \/>\nNSMutableDictionary *dic = [NSMutableDictionary dictionary];<br \/>\n[dic setObject:str1 forKey:@&quot;KEY1&quot;];<br \/>\n[dic setObject:str2 forKey:@&quot;KEY2&quot;];<\/p>\n<p>[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(yourmethod:) userInfo:dic repeats:NO];<\/p>\n<p>-(void)yourmethod:(NSTimer *)timer{<br \/>\n    NSMutableDictionary *dic = timer.userInfo;<br \/>\n    NSString *str1 = [dic objectForKey:@&quot;KEY1&quot;];<br \/>\n    NSString *str2 = [dic objectForKey:@&quot;KEY2&quot;];<br \/>\n}<br \/>\n[\/c]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>dispatch_async [c]dispatch_async(dispatch_get_global_qu<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=1964\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1964","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1964"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1964\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}