Objective-C Cheat Sheet Fully Updated!

Ray Wenderlich
Objective-C Cheat Sheet Fully Updated!

Objective-C Cheat Sheet Fully Updated!

This is a quick note to let you know that I’ve updated our Objective-C cheat sheet for ARC and new features in Xcode 4.4 such as auto-synthesis and literal syntax.

You can download the latest version of the cheat sheet here:

Objective-C Cheatsheet and Quick Reference v1.2

This is meant to be a handy reference for beginners to Objective-C who are familiar with other languages, but trying to get used to the new syntax.

Feel free to print this out or send it to your friends learning Objective-C.

I hope you all enjoy! :]

Ray Wenderlich

Ray is an indie software developer currently focusing on iPhone and iPad development, and the administrator of this site. He’s the founder of a small iPhone development studio called Razeware, and is passionate both about making apps and teaching others the techniques to make them.

When Ray’s not programming, he’s probably playing video games, role playing games, or board games.

User Comments

12 Comments

  • Big help for someone who's still learning, thanks a lot!
    qball13z
  • Hey Ray, cool sheet.

    Some would have complaints about your init example though.

    Code: Select all

    - (id)initWithParam:(anytype)param {
        if ((self = [super init])) {
            self.propertyName = param;
        }
    return self;
    }


    There's a lot of debate around the use of accessor methods within init methods, what's your stance?

    Here is a topic on SO, http://stackoverflow.com/questions/1927 ... it-dealloc
    cwagdev
  • Great! Thanks for sharing!
    hieplt
  • What about:
    Code: Select all

    NSArray *arr = @[obj1, obj2, obj2];
    NSDictionary *dict = @{@"key1": obj1, @"key2": obj2};
    kervich
  • @cwagdev: Yeah I'm aware of the debate on that, but I think the number of times that actually gets you into trouble in practice is very small, especially if you know what you're doing. Personally I think it's better not to confuse beginners with that.

    @kervich: Yeah I should have added that in as an alternative form in the comments like I did for the other. In the next update I'll add that, thanks :]
    rwenderlich
  • Another tweak you might want to make... it is considered bad style to name method parameters like this:

    Code: Select all

    - (void)methodWithParameterA:(int)a andB:(int)b;


    The "and" is unnecessary, and leaving it out makes the method call cleaner:

    Code: Select all

    - (void)methodWithParameterA:(int)a b:(int)b;


    The only case where "and" is good to use in a parameter name is if the method does two different things:

    Code: Select all

    - (void)methodThatDoesA:(int)a andThenDoesB:(int)b;
    Hollance
  • Hollance wrote:
    The only case where "and" is good to use in a parameter name is if the method does two different things:

    Code: Select all

    - (void)methodThatDoesA:(int)a andThenDoesB:(int)b;


    Then again, a method that does two things can arguably be a bad practice, personally if I find myself putting "and" in a method name I try and break it into two methods. But it's all a matter of opinion/preference, this stuff can start a holy war with the right people in the room ;)
    cwagdev
  • I heartily second kervich's suggestion. I'd also like to see the new typesafe enum macro, container indexing, fast enumeration, and variable boxing. It doesn't have to be just one page Ray. :)

    Also, Ray, what software did you use to create this? Pages and then export to pdf?

    Thanks!

    -LK
    LatencyK
  • LatencyK wrote:I'd also like to see the new typesafe enum macro, container indexing, fast enumeration, and variable boxing. It doesn't have to be just one page Ray. :)



    Me, too, since I don't know what LatencyK is talking about and it sounds juicy! As to the page count, Ray, I kept clicking around thinking there was a page two and I somehow missed it!

    And just to add value to the near-call on the religious debate :-) if you are single-stepping through the debugger during an init call you really see the difference in the religious left use of self.xxxx versus the religious right use of _xxxxx. After hoping around (religious left) I've standardized my style on the religious right. Here's a lazy-loading example, slightly tongue-in-cheek to keep the warring to a minimum:

    Code: Select all
    - (UIButton *)religiousRight
    {
        if ( ! _religiousRight ) {
             _religiousRight = [[UIButton alloc] init];
             [_religiousRight setText:@"I'm religiously right!"];
        }
       return _religiousRight;
    }


    And for general use: Is it new in Xcode 4.5 that you can often hit semi-colon when you're nearly through with a line and Xcode completes out the required closing brackets and whatnot that were autosuggested? For example, in the initializer above, _religiousRight = [[UIButton alloc] init is all you need to type before hitting semi-colon -- Xcode knows a closing bracket is needed and puts it in for you.

    -Jim
    tobinjim
  • Hollance wrote:Another tweak you might want to make... it is considered bad style to name method parameters like this:

    Code: Select all

    - (void)methodWithParameterA:(int)a andB:(int)b;


    The "and" is unnecessary, and leaving it out makes the method call cleaner:

    Code: Select all

    - (void)methodWithParameterA:(int)a b:(int)b;


    The only case where "and" is good to use in a parameter name is if the method does two different things:

    Code: Select all

    - (void)methodThatDoesA:(int)a andThenDoesB:(int)b;


    The style Apple uses is "with", so your cleaner call would be
    Code: Select all

    - (void)methodWithA:(int)a withB:(int)b;


    Do you not prefer that?
    tobinjim
  • The actual word Apple uses depends on what the method does. Sometimes "with" makes sense, but not always.
    Hollance
  • As a beginner that does look great, the method naming especially for me. I just wanted to point out it doesn't display correctly on the iPhone (don't know about the iPad), but I can only see 2 columns and I feel like there'll be one (or two) more. Can't zoom in or out to view more, probably to do with being a mobile view. Could this be made available as a downloadable PDF (if it isn't already)?

    disregard, I see the error of my ways....

    (I'd clicked on the image,not the pdf link!)
    madivad

Other Items of Interest

Ray's Monthly Newsletter

Sign up to receive a monthly newsletter with my favorite dev links, and receive a free epic-length tutorial as a bonus!

Advertise with Us!

Our Books

Our Team

Tutorial Team

  • Brian Broom
  • Julian Meyer

... 50 total!

Update Team

  • Ray Fix

... 15 total!

Editorial Team

... 23 total!

Code Team

  • Orta Therox

... 3 total!

Translation Team

  • Cosmin Pupaza
  • Jiyeon Seo

... 33 total!

Subject Matter Experts

  • Richard Casey

... 4 total!