{"id":554,"date":"2018-02-23T23:40:00","date_gmt":"2018-02-23T15:40:00","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/objective-c-%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86\/"},"modified":"2018-02-23T23:40:00","modified_gmt":"2018-02-23T15:40:00","slug":"objective-c-%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=554","title":{"rendered":"Objective C \u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Objective-C <span class=\"color_h1\">\u7b80\u4ecb<\/span><\/h1>\n<p>\u5728iOS\u7684\u5f00\u53d1\u4e2d\u4f7f\u7528\u7684\u662fObjective C\u8bed\u8a00\uff0c\u5b83\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\uff0c\u56e0\u800c\u5bf9\u4e8e\u5df2\u7ecf\u638c\u63e1\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u77e5\u8bc6\u7684\u7f16\u7a0b\u8005\u6765\u8bf4\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002<\/p>\n<hr>\n<h2>\u63a5\u53e3\u548c\u5b9e\u73b0<\/h2>\n<p>\u5728Objective\u91cc\u5b8c\u6210\u7684\u6587\u4ef6\u88ab\u79f0\u4e3a\u754c\u9762\u6587\u4ef6\uff0c\u8be5\u7c7b\u6587\u4ef6\u7684\u5b9a\u4e49\u88ab\u79f0\u4e3a\u5b9e\u73b0\u6587\u4ef6\u3002<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u754c\u9762\u6587\u4ef6MyClass.h\u5c06\u5982\u56fe\u6240\u793a:<\/p>\n<pre>\n@interface MyClass:NSObject{ \n\/\/ \u7c7b\u53d8\u91cf\u58f0\u660e\n}\n\/\/ \u7c7b\u5c5e\u6027\u58f0\u660e\n\/\/ \u7c7b\u65b9\u6cd5\u548c\u58f0\u660e\n@end\n<\/pre>\n<p>\u6267\u884cMyClass.m\u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n@implementation MyClass\n\/\/ \u7c7b\u65b9\u6cd5\u5b9a\u4e49\n@end\n<\/pre>\n<hr>\n<h2>\u521b\u5efa\u5bf9\u8c61<\/h2>\n<p>\u5b8c\u6210\u521b\u5efa\u5bf9\u8c61\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\nMyClass  *objectName = [[MyClass alloc]init] ;\n<\/pre>\n<hr>\n<h2>\u65b9\u6cd5\uff08methods\uff09<\/h2>\n<p>Objective C\u4e2d\u58f0\u660e\u7684\u65b9\u6cd5\u5982\u4e0b\u6240\u793a:<\/p>\n<pre>\n-(returnType)methodName:(typeName) variable1 :(typeName)variable2;\n<\/pre>\n<p>\u4e0b\u9762\u663e\u793a\u4e86\u4e00\u4e2a\u793a\u4f8b:<\/p>\n<pre>\n-(void)calculateAreaForRectangleWithLength:(CGfloat)length \nandBreadth:(CGfloat)breadth;\n\n<\/pre>\n<p>\u4f60\u53ef\u80fd\u4f1a\u60f3\u4ec0\u4e48\u662fandBreadth\u5b57\u7b26\u4e32\uff0c\u5176\u5b9e\u5b83\u7684\u53ef\u9009\u5b57\u7b26\u4e32\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9605\u8bfb\u548c\u7406\u89e3\u65b9\u6cd5\uff0c\u5c24\u5176\u662f\u5f53\u65b9\u6cd5\u88ab\u8c03\u7528\u7684\u65f6\u5019\u3002<\/p>\n<p>\u5728\u540c\u4e00\u7c7b\u4e2d\u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u6211\u4eec\u4f7f\u7528\u4e0b\u9762\u7684\u8bed\u53e5\u3002<\/p>\n<pre>\n[self calculateAreaForRectangleWithLength:30 andBreadth:20];\n<\/pre>\n<p>\u6b63\u5982\u4e0a\u6587\u6240\u8bf4\u7684andBreath\u4f7f\u7528\u6709\u52a9\u4e8e\u6211\u4eec\u7406\u89e3breath\u662f20\u3002Self\u7528\u6765\u6307\u5b9a\u5b83\u662f\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u7c7b\u65b9\u6cd5\uff08class methods\uff09<\/strong><\/p>\n<p>\u76f4\u63a5\u800c\u65e0\u9700\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u65b9\u6cd5\u3002\u4ed6\u4eec\u6ca1\u6709\u4efb\u4f55\u53d8\u91cf\u548c\u5b83\u5173\u8054\u7684\u5bf9\u8c61\u3002\u793a\u4f8b\u5982\u4e0b:<\/p>\n<pre>\n+(void)simpleClassMethod;\n<\/pre>\n<p>\u5b83\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7c7b\u540d\uff08\u5047\u8bbe\u4f5c\u4e3aMyClass\u7c7b\u540d\u79f0\uff09\u8bbf\u95ee\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n<pre>\n[MyClass simpleClassMethod];\n<\/pre>\n<p><strong>\u5b9e\u4f8b\u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u521b\u5efa\u7684\u7c7b\u7684\u5bf9\u8c61\u540e\u53ea\u8bbf\u95ee\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u5185\u5b58\u5206\u914d\u5230\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002\u5b9e\u4f8b\u65b9\u6cd5\u5982\u4e0b\u6240\u793a:<\/p>\n<pre>\n-(void)simpleInstanceMethod; \n<\/pre>\n<p>\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\u540e\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002\u5982\u4e0b\u6240\u793a:<\/p>\n<pre>\nMyClass  *objectName = [[MyClass alloc]init] ;\n[objectName simpleInstanceMethod];\n<\/pre>\n<hr>\n<h2>Objective C\u7684\u91cd\u8981\u6570\u636e\u7c7b\u578b<\/h2>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u5e8f\u53f7<\/th>\n<th>\u6570\u636e\u7c7b\u578b<\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>NSString\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>CGfloat \u6d6e\u70b9\u503c\u7684\u57fa\u672c\u7c7b\u578b <\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>NSInteger \u6574\u578b<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>BOOL \u5e03\u5c14\u578b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2>\u6253\u5370\u65e5\u5fd7<\/h2>\n<p>NSLog\u7528\u4e8e\u6253\u5370\u4e00\u4efd\u58f0\u660e\uff0c\u5b83\u5c06\u6253\u5370\u5728\u8bbe\u5907\u65e5\u5fd7\u548c\u8c03\u8bd5\u7248\u672c\u7684\u63a7\u5236\u53f0\u548c\u5206\u522b\u8c03\u8bd5\u6a21\u5f0f\u4e0a\u3002<\/p>\n<p>\u5982 NSlog(@&#8221;&#8221;);<\/p>\n<hr>\n<h2>\u63a7\u5236\u7ed3\u6784<\/h2>\n<p>\u9664\u4e86\u51e0\u4e2a\u589e\u8865\u7684\u6761\u6b3e\u5916\uff0c\u5927\u591a\u6570\u7684\u63a7\u5236\u7ed3\u6784\u4e0eC\u4ee5\u53caC++\u76f8\u540c<\/p>\n<p><strong>\u5c5e\u6027\uff08properties\uff09<\/strong><\/p>\n<p>\u7528\u4e8e\u8bbf\u95ee\u7c7b\u7684\u5916\u90e8\u7c7b\u7684\u53d8\u91cf\u5c5e\u6027<\/p>\n<p>\u6bd4\u5982\uff1a@property\uff08\u975e\u539f\u5b50\u3001\u5f3a\uff09NSString*myString<\/p>\n<p><strong>\u8bbf\u95ee\u5c5e\u6027<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u70b9\u8fd0\u7b97\u7b26\u8bbf\u95ee\u5c5e\u6027\uff0c\u82e5\u8981\u8bbf\u95ee\u4e0a\u4e00\u5c5e\u6027\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c<\/p>\n<pre>\nself.myString = @\"Test\";\n<\/pre>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528set\u7684\u65b9\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\n[self setMyString:@\"Test\"];\n<\/pre>\n<p><strong>\u7c7b\u522b\uff08categories\uff09<\/strong><\/p>\n<p>\u7c7b\u7528\u4e8e\u5c06\u65b9\u6cd5\u6dfb\u52a0\u5230\u73b0\u6709\u7c7b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u65b9\u6cd5\u6dfb\u52a0\u5230\u7c7b\uff0c\u751a\u81f3\u4e0d\u7528\u6267\u884c\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u5728\u5176\u4e2d\u5b9a\u4e49\u5b9e\u9645\u7684\u7c7b\u3002MyClass\u7684\u6837\u672c\u7c7b\u522b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\n@interface MyClass(customAdditions)\n- (void)sampleCategoryMethod;\n@end\n\n@implementation MyClass(categoryAdditions)\n\n-(void)sampleCategoryMethod{\n   NSLog(@\"Just a test category\");\n}\n<\/pre>\n<hr>\n<h2>\u6570\u7ec4<\/h2>\n<p>NSMutableArray \u548c NSArray \u662f ObjectiveC \u4e2d\u4f7f\u7528\u7684\u6570\u7ec4\u7c7b\uff0c\u524d\u8005\u662f\u53ef\u53d8\u6570\u7ec4\uff0c\u540e\u8005\u662f\u4e0d\u53ef\u53d8\u6570\u7ec4\u3002\u5982\u4e0b:<\/p>\n<pre>\nNSMutableArray *aMutableArray = [[NSMutableArray alloc]init];\n[anArray addObject:@\"firstobject\"];\nNSArray *aImmutableArray = [[NSArray alloc]\ninitWithObjects:@\"firstObject\",nil];<\/pre>\n<hr>\n<h2>\u8bcd\u5178<\/h2>\n<p>NSMutableDictionary\u548cNSDictionary\u662fObjective\u4e2d\u4f7f\u7528\u7684\u5b57\u5178\uff0c\u524d\u8005\u53ef\u53d8\u8bcd\u5178\uff0c\u540e\u8005\u4e0d\u53ef\u53d8\u8bcd\u5178\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n<pre>\nNSMutableDictionary*aMutableDictionary = [[NSMutableArray alloc]init];\n[aMutableDictionary setObject:@\"firstobject\" forKey:@\"aKey\"];\nNSDictionary*aImmutableDictionary= [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:\n@\"firstObject\",nil] forKeys:[ NSArray arrayWithObjects:@\"aKey\"]];\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Objective-C \u7b80\u4ecb \u5728iOS\u7684\u5f00\u53d1\u4e2d\u4f7f\u7528\u7684\u662fObjective C\u8bed\u8a00\uff0c\u5b83\u662f\u4e00\u79cd&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=554\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[74,93],"class_list":["post-554","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=554"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/554\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}