I am learning how to create iPhone apps and I have seen that most of the variables we create store memory addresses (pointers) instead than holding the actual value or object. I also have found out that every time you declare a variable with the pointer char (*) you know that the variable is going to hold the address and whenever you don’t use the (*) mark to declare a variable you know that it will hold the value instead than the memory location. But I don’t know when to us which. for example I have:
CGFloat someVar = [image1 alpha]; // This variable does not require *
// image 1 is a: IBOutlet UIImageView
and in this other case I have to use a pointer:
UIViewController *someOtherVar = [[UIViewController alloc] init]; // this type of var requires *
It will be nice if I can know when can I use each instead of trying each until project compiles.
The function and method signatures in the headers and documentation will indicate what the type is.
For example, here is how the
alphaproperty is declared forUIView:There is no
*anywhere, so you know it returnsCGFloatand notCGFloat*.In contrast, the
backgroundColorproperty is declared like this:so you know it will return
UIColor*(a pointer).Some things are declared with a type of
id, which is always going to be a pointer to an object.In general, Objective-C objects (types declared with
@interface) will always be referenced as pointers, while primitive C types andstructswill often (but not always) be passed and returned by value.