New Objective-C Tutorial
I just posted a new tutorial at Cocoa Dev Central. This one is called simply, Learn Objective-C. This tutorial is aimed at programmers who already have some basic experience with C and are looking to jump right into Objective-C.This is, of course, useful for a Mac development, but it's particularly useful right now for anyone who want to write iPhone software but isn't sure where to start. Learning Objective-C is one of the most basic needs you'll have, so hopefully this will help.
I should mention that this article is technically 95% done. I need to make some grammar and graphical corrections, but I wanted to let you guys and girls get started with it sooner rather than later.
If you don't know C already, you can read the C Tutorial first, then come back to this one.
I hope you like it. Enjoy.
New Objective-C Tutorial
Posted Apr 14, 2008 — 30 comments below
Posted Apr 14, 2008 — 30 comments below
Cathy Shive — Apr 14, 08 5716
Eimantas — Apr 14, 08 5717
Read it all. Nice. Noticed only a few typos, but will be waiting for updates ,)
Mike F. — Apr 14, 08 5718
When reading this, I noticed that in your section on Accessors (2), you state, "All instance variables are private in Objective-C by default."
However, in the Objective-C guide from Apple, they state, "By default, all unmarked instance variables (like name above) are @protected." (p.38)
That confused me slightly, but since you don't really mention the concept of protected vs. private variables in your tutorial, I'm also not sure whether it is an intentional simplification.
Thanks for your excellent work on the community's behalf!
Steven Degutis — Apr 14, 08 5719
- (BOOL) isURL { if ( [self hasPrefix: @"http://"] ) return YES; // else return NO; }
Steven Degutis — Apr 14, 08 5720
The instance variables are in fact protected, but this simply means that they are private to all external sources, but subclasses may access them.
For example, if I create a class Car that has an instance variable of type NSString called "model" then my subclass Lexus may access the "model" variable in its superclass Car, but any instance in the class GasStation cannot access that variable without using an accessor (getter).
Ewan — Apr 14, 08 5721
Ian Athanasakis — Apr 14, 08 5722
cesar — Apr 14, 08 5723
the new tutorial is pretty cool! just have one question. How do you create your graphics??
Scott Stevenson — Apr 14, 08 5724
I actually meant private in the general sense, but yes, you're right. I'll probably change that. There were a lot of thing I cut in order to keep it a reasonable length, and this was on of them.
@Steven Degutis: In the tutorial, at the end, you give an example of a category which implements the -isURL method on NSString
Thanks for pointing this out. Silly mistake on my part.
@Ewan: However, the introduction text says it's to replace bold tags with italics tags
I'll fix that too, thanks.
@cesar: How do you create your graphics?
Just Photoshop and Illustrator.
Andy Lee — Apr 14, 08 5726
Not to be pedantic -- and maybe you're trying to keep things simple -- but they aren't named arguments at all. This has been brought up in several places; I posted a link to a simple proof of this
on cocoa-dev recently.
Again, maybe you're trying to keep things simple, or you think "named arguments" is a close enough description for beginners (there's time later to fine-tune their understanding), but IMO it's better not to feed the misconception.
In any case -- thanks for the tutorial!
Paul Robinson — Apr 15, 08 5735
Jon Kantro — Apr 16, 08 5736
Andras Puiz — Apr 16, 08 5740
Danny — Apr 18, 08 5749
Marco Masser — Apr 19, 08 5750
Also, there are two errors in section 7 "More on Memory Management." The following line appears twice in the section:
NSNumber* value1 = [[Photo alloc] initWithFloat:8.75];
The +alloc method should be sent to the NSNumber class, of course.
And one small grammatical error: In section 9 "Properties," there's a verb ("feel"?) missing in the second paragraph, counted from the bottom.
Scott Stevenson — Apr 19, 08 5751
I agree about needing to call out the @"string" syntax. I need to figure out where it fits in the flow.
@Marco Masser: the fact that objects you create with methods other than -init... (e.g. [NSString string]) are simply autoreleased in the constructor by convention.
The trickiest topic in an entry-level Objective-C tutorial is memory management. I tried to simplify it down to its essence, and I think talking about the conventions behind factory methods would be too much detail. The mechanics don't change how they use the class, so I don't think it's an essential skill to get started.
Also, there are two errors in section 7 "More on Memory Management [...] And one small grammatical error: In section 9 "Properties,"
Both fixed. Thanks for finding them.
Marco Masser — Apr 20, 08 5754
OK, I agree on that point. It's just that I finally understood the elegance behind the autorelease mechanics only after I started to write my own classes with factory methods. At first I had memory leaks or double releases until I got my head around that convention. But as you said, that's probably too much detail for an entry-level Objective-C tutorial.
Marco Masser — Apr 20, 08 5755
[...] I think talking about the conventions behind factory methods would be too much detail.
OK, I agree on that point. It's just that I finally understood the elegance behind the autorelease mechanics only after I started to write my own classes with factory methods. At first I had memory leaks or double releases until I got my head around that convention. But as you said, that's probably too much detail for an entry-level Objective-C tutorial.
Marco Masser — Apr 20, 08 5756
I just found that the the -isURL method in the downloadable Xcode project is still the "old" one.
Danny — May 03, 08 5763
Scott Stevenson — May 04, 08 5767
Garbage-collected objects can implement -finalize. It's not a direct replacement for dealloc, but it will probably do what you need.
kamelito — May 08, 08 5805
We've to wait, I'm sure it'll worth it...
Check it out :
http://www.amazon.com/Programming-Objective-C-2-0-Developers-Library/dp/0321566157/ref=sr_1_3?ie=UTF8&s=books&qid=1210279077&sr=1-3
Joe — May 23, 08 5895
Can you explain further how copy and retain work?
Thanks ahead of time; I'm having a hard time getting this one.
Scott Stevenson — May 24, 08 5910
Do you have a specific question? I'm not sure if I could explain in more clearly here as a comment than I did in the actual tutorial. Does the concept not make sense at all, or is there one part you're stuck on?
Joe — May 25, 08 5915
retain increments the reference count of an object every time it's called. And including it as an attribute in the property declaration is the equivalent of
[[caption setCaption:newCaption]retain];
so retain is called every time the setter is called?
And copy is another way to create an object and would look like
newObject = [oldObject copy];
and the new object comes with a reference count of 1?
Joe — May 28, 08 5943
I didn't understand why retain would be called over and over.
So, does including retain in the property declaration mean that the autorelease > retain sequence will be synthesized? or is that synthesized by default? in which case, what would retain do?
Specifically, I'm having trouble understanding the difference between retain and assign in property declarations.
I've looked at the documentation on this several times, and it's kind of vague (for me).
Scott Stevenson — May 29, 08 5948
If the instance variable is called "myName", assign would do something roughly equivalent to this:
myName = newName;
Retain would do this:
[myName autorelease]; myName = [newName retain];
Copy would do this:
[myName autorelease]; myName = [newName copy];
This isn't necessarily the exact code that gets generated, but it has the same basic effect.
You also asked if the retain keyword would be the equivalent of "[[caption setCaption:newCaption]retain];". The answer is no, but I'm not sure how to quickly explain why that is the case. I hope the above clears that up.
If you have garbage collection on, assign and retain are the same thing.
Jason Barry — Jul 02, 08 6136
"Avoid nested nesting more than two message calls on a single line, as it easily gets unreadable."
I believe the second word nested should be removed. Thanks for the great tutorial.
Paul — Aug 14, 08 6247
I wanted to thank you for post this tutorial on Objective-C. I was just asked to write an app for the Iphone and I've never used objective-c or cocoa before...in fact I've never booted up a Mac computer before, so its all new to me right now :)
Dan Ribar — Jun 05, 09 6803
One of the things i'm stuck on is [using your example] that I can't set and then read a variable from an outside class. It keeps going back to null....
So if I set 'caption' from one method
and try to get the value that is on 'caption' from another method, it is gone. I know there is discussion above, but i'm not too sure what the resolution is.
I am using something like this to read the 'caption' value:
method1============================
photo* myPhoto = [[photo alloc]init];
myPhoto.caption=@"some caption";
NSLog(@"caption: %@",myPhoto.caption);
==================================
in this example all is well.
now in the same class I try this method and the value is now null
method2============================
NSLog(@"caption: %@",myPhoto.caption);
==================================
dude - not too sure what I'm missing. Must be part of the asp.net to objective-c cycle... arghhh..
any thoughts?
Dan Ribar