Question

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.

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top