开发者

Want to learn Objective-C but syntax is very confusing

Coming from Java background I am guessing this is expected. I would really love to learn Objective-C and start developing Mac apps, but the syntax is just killing me.

For example:

-(void) setNumerator: (int) n
{
    numerator = n;
}

Wha开发者_如何转开发t is that dash for and why is followed by void in parenthesis? I've never seen void in parenthesis in C/C++, Java or C#. Why don't we have a semicolon after (int) n? But we do have it here:

-(void) setNumerator: (int) n;

And what's with this alloc, init, release process?

myFraction = [Fraction alloc]; 
myFraction = [myFraction init];
[myFraction release];

And why is it [myFraction release]; and not myFraction = [myFraction release]; ?

And lastly what's with the @ signs and what's this implementation equivalent in Java?

@implementation Fraction

@end

I am currently reading Programming in Objective C 2.0 and it's just so frustrating learning this new syntax for someone in Java background.

UPDATE 1: After reading all these answers and Programming in Objective C 2.0 a little further, it's all starting to make sense now!

UPDATE 2: Thanks for the great tip "support-multilanguage". We can combine these two statements into one like so:

myFraction = [Fraction alloc]; 
myFraction = [myFraction init];

Can be reduced to:

myFraction = [[Fraction alloc] init]; 


What is that dash for and why is followed by void in parenthesis?

The dash - means "instance level" ( which is the default in Java ). When you see a plus sign + it means class level ( same as using static in Java )

The void in parenthesis is the return type see below.

Why don't we have a semicolon after (int) n? But we do have it here:

-(void) setNumerator: (int) n;

Because that's the method signature, you can think of it as a interface:

 interface YourClass {
      void setNumerator(int n );// see? semicolon
 }

Same situation.

Objective-C having roots in C ( actually this is the real C with objects ) needs a header file ( completely removed in Java ) where you define what the functions/methods your class would have.

The equivalent if such thing would exist in java would be:

// YourClass.java.h 

 interface YourClass {
      void setNumerator(int n );
 }
 // YourClass.java.m
 class YourClass {
      void setNumerator( int n ) {
       this.n = n;
      }
  }

And what's with this alloc, init, release process?

myFraction = [Fraction alloc]; 
myFraction = [myFraction init];
[myFraction release];

allo-init is the equivalent for new in Java, alloc that's when you ask for memory for your object, alloc request memory, and init calls the init method, equivalent to the Java class constructor ( btw Ruby does the same behind scenes )

There's no equivalent to release in Java because it is garbage collected, in Objective-C you have to release your object.

BTW, the initialization could also be myFraction = [[Fraction alloc]init];

And lastly what's with the @ signs and what's this implementation equivalent in Java?

 @implementation Fraction

 @end

That's the class definition as I mentioned earlier.

I am currently reading Programming in Objective C 2.0 and it's just so frustrating learning this new syntax for someone in Java background.

Here's a related answer that will help you to get more familiar with the square brackets:

Brief history of the "square brackets" is I remember it.


Perhaps have a wizz through the wiki page for Objective-C. I just did and found most of your answers.

http://en.wikipedia.org/wiki/Objective-C

I'm from a java background too, on first look objectivec doesn't look too sexy.

It looks like C++ with little bits of syntax on it's face.

What is that dash for and why is followed by void in parenthesis?

minus here denotes an instance method, a plus denotes a class method

I've never seen void in parenthesis in C/C++, Java or C#.

this is the return type

Why don't we have a semicolon after (int) n?

If you don't provide a method body then the syntax is a method declaration - not an implementation.

And lastly what's with the @ signs and what's this implementation equivalent in Java?

@implementation Fraction

@end

in java would probably be

public class Fraction {

}


Coming from the same book, the "-" sign tells the compiler that the method is an instance method (one that modifies data). A "+" sign however tells the computer that it is a class (static) method.


You said you are reading "Programming in Objective C 2.0" by Stephen Kochan. It may not be as quick as you would like but some time spent heads down with that book will serve you well. Everything you are asking is addressed in the Kochan book. You might supplement that with what you find on the Apple site, in Wikipedia, and on the O'Reilly MacDevCenter.com such as "Inside the Objective-C Runtime." Objective C's dynamic implementation of objects is a bit different that Java or C++ and has a different look to it, perhaps due to the Smalltalk-inspired approach. Go with Kochan, supplement that with the web, and see if key sticking points have been vetted here. If not, this will be a great place for them.


I'm also just getting started with Objective C and I agree about the syntax seeming foreign relative to Java or C++. The best thing I've found is to just read through some example code. I'm sure you can find plenty with a bit of googling. I'm not allowed to post more than one link, but if you google "objective c example code" you should find plenty of code.

If you are just looking for a quick reference to what some of the weird syntax means, this is a pretty good tutorial:

http://cocoadevcentral.com/articles/000082.php


If all that just gets you down, start learning Swift! Does all the cool stuff that obj-c does but with much more recognisable syntax. And they reckon its faster too. Available if you have an apple dev account - get XCode 6 / iOS8 beta.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜