views:

74

answers:

3

So I'm trying to make the horrible leap from VB.NET to objective-C.

My only curly-brace experience is a little ActionScript 3... I'm having a hard time grasping the use of the * character.

Question 1:

The tutorial I'm using has these two lines next to each other..

IBOutlet UIPickerView *pickerView;
NSArray* myArray;

It's my understanding that the * denotes essentially a reference type. Why does it precede the variable name on the first line, but the class name on the second?

Question two, about class declarations:

@implementation {
 Why is some code here
}

and other code here?
@end
+6  A: 
  1. That's a big can of worms - the answer is that the syntax there is equivalent, you can do whichever you like. Some people are pretty religious about one or the other, but the compiler doesn't care.

  2. I think you mean @interface, right? The code inside the braces is just a list of instance variables. Outside the braces is where you put method declarations.

If you want to learn Objective-C, it is probably well worth it to go and learn C first. Then when you understand the syntax and C concepts like pointers and forward declarations, you can pick up Objective-C pretty quickly.

Carl Norum
One thing to keep in mind is that if you're declaring multiple variables per line (`NSArray *oneArray, *anotherArray;`) you need the `*` for each variable (`NSArray* oneArray, anotherArray;` is like `NSArray * oneArray; NSArray anotherArray;`). This might be a good reason to keep the `*` with the variable name. Or, if you prefer the `*` with the type (as I do), then it's a good reason to avoid ever declaring more than one variable per line. :)
andyvn22
@andyvn22 Absolutely agree.
Jacob Relkin
+1 @andyvn22. That's why there are religious debates.
Carl Norum
+2  A: 

Where you stick the * in a variable declaration doesn't matter so long as it is after the type.

This is actually wrong:

@implementation x {
}

@end

You can only do that with @interface:

@interface x {
  //ivars here...
}

//method declarations here...

@end
Jacob Relkin
A: 

Whitespace around the "*" is irrelevant, so to the compiler the two lines you gave are identical - two pointers to objects (reference types as you called them).

Basically objects generally need "", where primitive types (and things like struct) do not - so for instance a CGRect would not use a "".

However just to confuse you a little more, there is a generic reference to an object "id" that does not need (and cannot have) a * - you would declare a variable like that as:

id delegate;
Kendall Helmstetter Gelner