Objective-C基础课问题

发布于 2024-07-29 05:19:28 字数 315 浏览 6 评论 0原文

因此,我对重新编程有点生疏,而且我似乎找不到一个很好的参考来理解我想要实现的目标的结构。 因此,言归正传,我正在研究创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-08-05 05:19:28

我建议阅读(至少部分内容)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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文