File tree Expand file tree Collapse file tree 2 files changed +9
-11
lines changed
Expand file tree Collapse file tree 2 files changed +9
-11
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ Cocoa 库本身在一些地方也使用了单例模式,例如`[NSNotificationC
3434
3535可以使用@synchronized进行加锁,代码如下:
3636
37- ```
37+ ```objectivec
3838/* Singleton.h */
3939#import <Foundation/Foundation.h>
4040@interface Singleton : NSObject
@@ -60,15 +60,15 @@ static Singleton *instance = nil;
6060
6161这里主要利用GCD中的dispatch_once方法,这是最普遍也是苹果最推荐的方法,函数原型如下:
6262
63- ```
63+ ``` objectivec
6464void dispatch_once (
6565 dispatch_once_t * predicate,
6666 dispatch_block_t block);
6767```
6868
6969单例实现代码如下:
7070
71- ```
71+ ```objectivec
7272/* Singleton.h */
7373#import <Foundation/Foundation.h>
7474@interface Singleton : NSObject
@@ -309,5 +309,4 @@ int main(int argc, const char * argv[]) {
309309* [详解Objective-C中的委托和协议](http://mobile.51cto.com/iphone-283416.htm)
310310* [Objective-C中Observer模式的实现](http://blog.csdn.net/zshtiger2414/article/details/6409695)
311311* [Objective-C KVO编程](http://blog.csdn.net/kindazrael/article/details/7961601)
312- * [ iOS开发系列——Objective-C开发之KVC,KVO] ( http://www.cnblogs.com/kenshincui/p/3871178.html )
313- * [ 浅谈iOS设计模式之单例模式] ( http://ijack.pw/2016/01/19/iOS设计模式之单例模式/ )
312+ * [iOS开发系列——Objective-C开发之KVC,KVO](http://www.cnblogs.com/kenshincui/p/3871178.html)
Original file line number Diff line number Diff line change @@ -34,23 +34,23 @@ Objective-C中提供了两种内存管理机制:MRC(MannulReference Counting
3434
3535如下是四个黄金法则对应的代码示例:
3636
37- ```
37+ ``` objectivec
3838/*
3939 * 自己生成并持有该对象
4040 */
4141 id obj0 = [[NSObeject alloc ] init ];
4242 id obj1 = [NSObeject new ];
4343```
4444
45- ```
45+ ``` objectivec
4646/*
4747 * 持有非自己生成的对象
4848 */
4949id obj = [NSArray array ]; // 非自己生成的对象,且该对象存在,但自己不持有
5050[obj retain ]; // 自己持有对象
5151```
5252
53- ```
53+ ``` objectivec
5454/*
5555 * 不在需要自己持有的对象的时候,释放
5656 */
@@ -62,7 +62,7 @@ id obj = [[NSObeject alloc] init]; // 此时持有对象
6262 */
6363```
6464
65- ```
65+ ``` objectivec
6666/*
6767 * 非自己持有的对象无法释放
6868 */
@@ -72,7 +72,7 @@ id obj = [NSArray array]; // 非自己生成的对象,且该对象存在,但
7272
7373其中 ` 非自己生成的对象,且该对象存在,但自己不持有 ` 这个特性是使用` autorelease ` 来实现的,示例代码如下:
7474
75- ```
75+ ``` objectivec
7676- (id ) getAObjNotRetain {
7777 id obj = [[NSObject alloc] init]; // 自己持有对象
7878 [obj autorelease]; // 取得的对象存在,但自己不持有该对象
@@ -473,5 +473,4 @@ Allocations 工具主要用来检测 Abandoned memory. 主要思路是在一个
473473* https://stackoverflow.com/questions/17601274/arc-and-autorelease
474474* https://stackoverflow.com/questions/8292060/arc-equivalent-of-autorelease
475475* https://stackoverflow.com/questions/7906804/do-i-set-properties-to-nil-in-dealloc-when-using-arc
476- * [ ARC中的Trick] ( http://ijack.pw/2016/03/17/ARC-naive/ )
477476* http://wereadteam.github.io/2016/02/22/MLeaksFinder/?from=singlemessage&isappinstalled=0
You can’t perform that action at this time.
0 commit comments