Objective-C 懒加载没有调用?怎么办

2019-11-07  本文已影响0人  MacleChen

可能错误原因

1. 是否保证变量是用@property修饰
2. 是否保证类加载方法中使用“_”下划线的变量,且做判断空处理
3. 是否保证调用的使用要用“self”的点语法调用

@property 系统已经帮我们创建好了getter和setter方法, 我们所做的就是覆盖系统创建的方法

懒加载正确编码

/// 文件目录
@property(nonatomic, copy) NSString *documentDir;

/// 懒加载
- (NSString *)documentDir {
    if (!_documentDir) {
        _documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    }
    
    return _documentDir;
}

- (void)mytestFunc {
   NSString *path = [self.documentDir stringByAppendingPathComponent:@"fmdb.db"];
}
上一篇 下一篇

猜你喜欢

热点阅读