讓兩個物件間建立weak關係

2021-09-06 18:25:52 字數 2793 閱讀 8890

讓兩個物件間建立weak關係

這是為了給兩個物件間建立weak關係,當乙個物件被釋放時,另外乙個物件再獲取這個值時就是nil,也就是不持有這個物件:)

原始碼:weakrelateddictionary.h 與 weakrelateddictionary.m

//

//testdemo

////

created by youxianming on 14-9-25.

////

#import

@inte***ce

weakrelateddictionary : nsobject

+ (void)addobject:(id)object forkey:(id

)key;

+ (id)objectforkey:(id

)key;

@end

//

//testdemo

////

created by youxianming on 14-9-25.

////

#import

"weakrelateddictionary.h

"static nsmaptable *weakdictionary =nil;

@implementation

weakrelateddictionary

+ (void

)initialize

}+ (void)addobject:(id)object forkey:(id

)key

if ([weakdictionary objectforkey:key] ==nil)

}+ (id)objectforkey:(id

)key

@end

nsobject+weakrelated.h 與 nsobject+weakrelated.m

//

//nsobject+weakrelated.h

//scrollviewshowimage

////

created by youxianming on 14-9-24.

////

#import

@inte***ce

nsobject (weakrelated)

//與物件弱相關聯絡

- (void)setrelatedobject:(id)object;//

取出建立關係的物件

- (id

)relatedobject;

@end

//

//nsobject+weakrelated.m

//scrollviewshowimage

////

created by youxianming on 14-9-24.

////

#import

"nsobject+weakrelated.h

"#import

"weakrelateddictionary.h

"@implementation

nsobject (weakrelated)

- (void)setrelatedobject:(id)object

- (id

)relatedobject

@end

測試**:

model.h 與 model.m

//

//objectrelated

////

created by youxianming on 14-9-27.

////

#import

@inte***ce

model : nsobject

@end

//

//objectrelated

////

created by youxianming on 14-9-27.

////

#import

"model.h

"@implementation

model

@end

viewcontroller.m

//

//objectrelated

////

created by youxianming on 14-9-27.

////

#import

"viewcontroller.h

"#import

"nsobject+weakrelated.h

"#import

"weakrelateddictionary.h

"#import

"model.h

"@inte***ce

viewcontroller ()

@property (nonatomic, strong) model *model;

@end

@implementation

viewcontroller

- (void

)viewdidload

- (void

)showinfo

@end

如何判斷兩個現象間存在因果關係

1.兩件事 a與b 相互關聯 association 2.a的發生可以 b的發生 prediction 3.排除其他可能的混淆變數 excluding confounding variable 所以可以看出,判斷因果其實是個邏輯過程。首先要證明兩個事件有關聯 association 這種關聯一般來說...

取得兩個間的Strings

在google上面看到的.也許有人有用.ff cccddd eee11 jkjl 1.print join map grep 會列印出 ff ccc ddd eee 11jkjl 2.不包含 print join map grep and and m 列印出ccc ddd eee 3.不包含 pri...

讓兩個Div併排顯示

一 使用display的inline屬性 aaaa bbbb 二 通過設定float來讓div併排顯示 1111 2222 2222 2222 三 對於兩個div併排,左邊為絕對寬度,右邊為相對寬度的,需要用到這種布局的情況比較多見,如左邊為導航,右邊為內容的頁面 1 將最大的容器padding l...