Objective-C基础课问题
因此,我对重新编程有点生疏,而且我似乎找不到一个很好的参考来理解我想要实现的目标的结构。 因此,言归正传,我正在研究创建 Class 对象,例如。
#import Assets.h
@interface MainRecord: NSObject {
Assets* assets;
...
}
...
@end
在类中拥有类对象,在 main 中创建该对象时是否需要初始化? 我想确保为 MainRecord 创建的每个实例始终与其资产相关联。(将来这些将写入文件)所有这些主要是为了可读性以及向此类添加对象的方便性。
So I'm a bit rusty getting back into programming and I can't seem to find a good reference for understanding the structure for what I am trying to achieve. So, without further ado I am looking at creating and Class object such as.
#import Assets.h
@interface MainRecord: NSObject {
Assets* assets;
...
}
...
@end
Having a class object within a class, do i need to initialize when the object is created in main? I want to make sure each instance created for MainRecord will always be associated with it's Assets.(in the future these will be written to a file) All of which is mainly for readability and ease of adding objects to this class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议阅读(至少部分内容)Objective-C 2.0 编程语言,Apple 发布的指南。 名为 “定义类” 的部分将回答你的大部分问题。
基本上,您不会在
main()
中初始化实例变量——该类定义了处理其自身变量的方法。 (这是面向对象编程语言的常见做法。)在 Objective-C 中,你在-(id)init
方法中初始化实例变量,并在-(void)dealloc
方法中释放它们避免内存泄漏。 例如,请参阅 NSString。I recommend reading (at least parts of) The Objective-C 2.0 Programming Language, a guide published by Apple. The section called "Defining a Class" will answer the bulk of your questions.
Basically, you don't initialize instance variables in
main()
— the class defines methods that handle its own variables. (This is common practice for object-oriented programming languages.) In Objective-C you initialize instance variables in an-(id)init
method and release them in-(void)dealloc
method to avoid leaking memory. For example, see all the-initWith...
methods in NSString.