我有3个课程:function myFunction(){
var tableStr = `<table id="myTable">
<tr class="header"></tr>
<tr><td>Germany</td></tr>
<tr><td>Sweden</td></tr>
<tr><td>UK</td></tr>
<tr><td>Germany</td></tr>
<tr><td>Canada</td></tr>
<tr><td>Italy</td></tr>
<tr><td>UK</td></tr>
<tr><td>France</td></tr>
</table>`;
document.getElementById('tableContainer').insertAdjacentHTML('beforeend', tableStr)
}
,Core
和Cache
在HttpClient
中,我使用参数Core
初始化缓存,然后将core
实例传递给Cache
。
Core.m
HttpClient
Cache.m
-(id)init {
self = [super init];
MyCache *cache = [[MyCache alloc] initWithCore:self];
self.httpClient = [[MyHttpClient alloc] initWithCache:cache];
}
MyHttpClient.m
@interface MyCache ()
@property (nonatomic, strong) MyHttpClient *httpClient;
@end
@interface Core ()
@property (nonatomic, strong) MyHttpClient *httpClient;
@end
@implementation MyCache
-(instancetype) initWithCore: (Core *)core
{
if (self = [self init]) {
self.httpClient = core.httpClient;
}
return self;
}
@end
-(void)foo
{
[self.httpClient doSomething];
}
您可以看到,@interface MyHttpClient : NSObject
@property (nonatomic, strong) MyCache *cache;
-(instancetype) initWithCache: (MyCache *)cache;
@end
@implementation MyHttpClient
-(instancetype) initWithCache: (MyCache *)cache
{
if (self = [self init]) {
self.cache = cache;
}
return self;
}
-(void) doSomething
{
[self.cache cacheSomething];
}
通过依赖注入使用MyCache
实例,而另一方面MyHttpClient
实例使用MyHttpClient
实例。
这是否称为保留周期?
我是否需要将MyCache
或MyCache
属性设为弱?
感谢,