Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
An introduction to Objective-C (thinkvitamin.com)
43 points by fogus on Sept 27, 2011 | hide | past | favorite | 12 comments


This tutorial is riddled with dangerous errors even though it's pretty basic. To start with, here's his initializer:

  - (id) initWithTitle: (NSString *)aTitle{
    self = [super init];
    if (self) {
      title = aTitle;
    }
    return self;
  }
aTitle isn't retained (and no, he's not using ARC). This is such an obvious, basic mistake that I have no faith in the author at this point.

Here's another error:

  For example, this is valid code:
  NSObject obj = [Book createBookWithTitle:@"A Brave New World"];
That's not true. It's missing the pointer (NSObject *, not NSObject). Furthermore it's otherwise syntactically valid but comes dangerously close to violating Cocoa best practices for memory management. The method (which returns an autoreleased result in their examples) should be named "bookWithTitle:".

Putting "create" in the name doesn't actually violate the memory management rules (in Cocoa, only methods that have alloc, new, copy, or mutableCopy return owning references). But it will cause confusion with the Create Rule from Core Foundation:

  Object-creation functions [have] “Create” embedded in the name...
  it is your responsibility to relinquish ownership (using CFRelease) when you have finished with it
Sure, Core Foundation is not Cocoa. But since it's often intermixed, sometimes explicitly with toll-free bridging, boy are people going to be confused someday!


Looking over it again, I don't think there's a single code example in there without an error. Did he even bother to try compiling this example before posting? What is it with dangerously bad Objective-C resources for beginners?


You're correct, the tutorial isn't good (and it is interesting to see that the website's revenue model is through their tutorials)

However, the tutorial is now #3 on the front page.


whoa you're right. this is littered with problems; the section on properties is screwy too


This is the author here. I have fixed those issues. I had just written the code and post in TextMate using markdown. I should have checked the code before I put it up there. My bad. As for putting create. I wanted to illustrate that they are creating a new object and don't stress on using create as naming convention.


Heh, as a person that loves Cocoa but doesn't own an iOS device (ok, I do have an iPod touch I got for free with my MBP), initializers like that are all over my code.


initializers should look like that. the problem is this:

title = aTitle;

he should say self.title instead to avoid memory issues.


Oh, what I meant is that on the Mac we can use garbage collection, so it's not always a problem.

I don't really like the property dot syntax though, I always use the `setProperty:` form for consistency (is it a struct or an object? why is the = operator sending an Obj-C message?)


I wrote a longer Obj-C & Cocoa getting started guide (about 30 pages) that's free and available here if anyone is interested: http://designthencode.com/scratch/


wow Mike, very nice. Cheers.


Please, start with Scott Stevenson's tutorials instead:

http://cocoadevcentral.com/


First two images with basic explanations what is what is most valuable for beginner.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: