目标C中的延迟加载 [英] Lazy loading in objective C
本文介绍了目标C中的延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我听说延迟加载技术对提高程序性能很有帮助.我正在为iPhone开发游戏.我不确定如何在目标C中应用延迟加载.有人可以向我展示该示例吗?
I heard lazy loading technique quite helpful to increase the performance of the programme. I am developing games for iPhone. I am not sure how is the way to apply lazy loading in objective C. Could anyone show me the example please?
预先感谢
推荐答案
延迟加载的一般模式始终大致相同:
The general pattern for lazy loading is always more or less the same:
- (Whatever *)instance
{
if (_ivar == nil)
{
_ivar = [[Whatever alloc] init];
}
return _ivar;
}
- 在您的类中,添加所需类型的ivar,然后在构造函数中将其初始化为nil;
- 为该ivar创建一个getter方法;
- 在吸气剂中测试零.如果是这样,请创建该对象.否则,只需返回对其的引用即可.
这篇关于目标C中的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文