开发者

New to Objective-C --> Quick question(s)

So I'开发者_StackOverflow社区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


  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.


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜