如何设置和访问其他类的变量值

发布于 2024-09-02 20:31:26 字数 304 浏览 5 评论 0原文

我正在修改 QuartzDemo 示例应用程序,并希望在已包含的其他类中设置和读取整数类型变量的值。

例如,在 MainViewController.m 中,我想将一个数值(1-100 之间的简单数字)设置为一个变量,然后该变量将在文件 QuartzImages.m 中调用(读取)。

问题是如何正确定义变量、设置和访问(读取)值。

我是 Obj C 和 iPhone SDK 的初学者,并且对 Delphi 和 VB 有一些经验,但这根本没有帮助:)

谢谢你,无论你是谁,无论你在哪里,都会带我摆脱这个无休止的谷歌搜索循环的人。

I am modifying QuartzDemo example app and want to do set and read values of integer type variables in other classes which are already included.

For example, in MainViewController.m I want to set a numeric value (simple numbers from 1-100) to a variable which is then going to be called (read) in file QuartzImages.m.

Question is how to define variable properly, set and access (read) the value.

I am a beginner with Obj C and iPhone SDK in general and have some experience with Delphi and VB but this doesn't help at all :)

Thank you, whoever and wherever you are, person who will take me out of this endless googling loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

私野 2024-09-09 20:31:26

如果你有一个变量,有两种方法可以读取和设置属性(通常是 ivar 和访问器方法的组合)。旧方法(仍然是某些人的首选)是直接使用访问器方法:

//MYObject *obj, with int property foo
int i = [obj foo]
[obj setFoo:32]

新方法是使用点语法(仅适用于属性,不能与更复杂的方法一起使用):

int i = obj.foo //equivalent to [obj foo]
obj.foo = 32 //equivalent to [obj setFoo:32]

如果这让您感到困惑,我强烈建议您阅读Aaron Hillegass 的书。它非常详细地解释了 Cocoa 和 Objective-C。

编辑:我认为这里的一些混乱在于术语。在 Objective-C 中,大多数对象都有实例变量(ivars),它们实际上存储数据。然而,一般来说,您希望避免直接访问 ivars,尤其是从其他对象。 (这是面向对象设计的原则,并不是 Objective-C 特有的。)

这样做的主要原因是为了提高灵活性——如果我有一个类 Person ,它有一个实例变量age,例如,我可能希望将来更改我的实现以动态确定年龄。如果其他类依赖于实例变量age,那么当实现发生变化时它们就会中断。

解决这个问题的方法是使用访问器,它是控制对 ivar 访问的方法。在 Objective-C 中,“getter”获取变量,并且通常命名为与变量相同的名称(因此在 Person 示例中,getter 为 -(int)age-(int)age code>)和“setters”设置变量(它将被命名为-(void)setAge:(int)theAge)。如果您使用访问器,您可以在将来自由地更改您的实现,甚至在必要时完全摆脱 ivar,而不会破坏依赖的类。

在 Objective-C 中,ivar 和访问器的组合通常称为“属性”(尽管如果值是动态创建的,则不一定必须有 ivar)。如果您从属性角度思考,则不必担心其他类中的 ivars——它们是实现细节。 Objective-C 2.0 有一些很好的语法糖来创建属性:

//Person.h
@interface Person : NSObject {
    int age;
}

@property (assign) int age;  //declares the accessors

@end

//Person.m
@implementation Person

@synthesize age; //implements -(int)age and -(void)setAge:theAge

@end

要记住的重要一点是,大多数时候,您应该考虑属性而不是 ivars。你永远不应该直接访问其他类中的ivars。您应该很少(如果有的话)访问超类中的 ivar。如果你是一个纯粹主义者,你甚至不应该直接访问同一个类中的ivars,除了访问器、initdealloc(后两者是有争议的) )。

If you have a variable, there are two ways to read and set properties (which are usually a combination of an ivar and accessor methods). The old way (still preferred by some) is to directly use the accessor methods:

//MYObject *obj, with int property foo
int i = [obj foo]
[obj setFoo:32]

The new way is to use dot-syntax (which only works for properties, and can't be used with more complicated methods):

int i = obj.foo //equivalent to [obj foo]
obj.foo = 32 //equivalent to [obj setFoo:32]

If this is confusing you, I'd highly recommend picking up a copy of Aaron Hillegass's book. It explains Cocoa and Objective-C in great detail.

EDIT: I think some of the confusion here is with terminology. In Objective-C, most objects have instance variables (ivars), which actually store data. However, in general you want to avoid directly accessing ivars, especially from other objects. (This is a principle of object-oriented design, and isn't really specific to Objective-C.)

The main reason for this is to increase flexibility--if I have a class Person that has an instance variable age, for instance, I may want to change my implementation in the future to dynamically determine the age. If other classes rely on the instance variable age, they will break when the implementation changes.

The way around this problem is to use accessors, which are methods that control access to an ivar. In Objective-C, "getters" get the variable, and are usually named the same thing as the variable (so in the Person example, the getter would be -(int)age) and "setters" set the variable (it would be named -(void)setAge:(int)theAge). If you use accessors, you can freely change your implementation in the future, even getting rid of the ivar completely if necessary, without reliant classes breaking.

In Objective-C, the combination of an ivar and accessors is often called a "property" (although there doesn't necessary have to be an ivar, if the value is dynamically created). If you think in terms of properties, you don't have to worry about ivars in other classes--they're implementation details. Objective-C 2.0 has some nice syntactic sugar for creating properties:

//Person.h
@interface Person : NSObject {
    int age;
}

@property (assign) int age;  //declares the accessors

@end

//Person.m
@implementation Person

@synthesize age; //implements -(int)age and -(void)setAge:theAge

@end

The important thing to remember is most of the time, you should think in terms of properties rather than ivars. You should never directly access ivars in other classes. You should rarely (if ever) access ivars in superclasses. If you're a purist, you shouldn't even directly access ivars in the same class except in accessors, init, and dealloc (and the latter two are subject to debate).

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