0% found this document useful (0 votes)
95 views1 page

Objective-C Cheat Sheet and Quick Reference: Superclass

This document provides a cheat sheet and quick reference for Objective-C. It summarizes key concepts like class headers, creating objects, properties, declaring variables, defining and implementing methods, and examples for NSString and NSArray. The summary also outlines attributes for defining properties and how to call and use methods and properties in Objective-C code.

Uploaded by

Lauren Moore
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views1 page

Objective-C Cheat Sheet and Quick Reference: Superclass

This document provides a cheat sheet and quick reference for Objective-C. It summarizes key concepts like class headers, creating objects, properties, declaring variables, defining and implementing methods, and examples for NSString and NSArray. The summary also outlines attributes for defining properties and how to call and use methods and properties in Objective-C code.

Uploaded by

Lauren Moore
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Objective-C Cheat Sheet and Quick Reference

Class Header (.h)


! #import "AnyHeaderFile.h" @interface ClassName : SuperClass // define public properties // define public methods @end

Creating an Object
! ClassName * myObject = [[ClassName alloc] init];

What is a Property?
1) Automatically defines a private instance variable:

type _propertyName;
2) Automatically creates a getter and setter:

Calling a Method
! [myObject doIt]; [myObject doItWithA:a]; [myObject doItWithA:a b:b];

- (type)propertyName; - (void)setPropertyName:(type)name;
Using _propertyName uses the private instance variable directly. Using self.propertyName uses the getter/setter.

Class Implementation (.m)


! #import "YourClassName.h" @interface ClassName () // define private properties // define private methods @end @implementation ClassName { // define private instance variables } // implement methods @end

Declaring Variables
type myVariable; Variable types int float double BOOL ClassName * id !
1, 2, 500, 10000 1.5, 3.14, 578.234

Custom Initializer Example


! - (id)initWithParam:(type)param { if ((self = [super init])) { _propertyName = param; } return self; } !

YES, NO
NSString *, NSArray *, etc. Can hold reference to any object

NSString Quick Examples


! NSString *personOne = @"Ray"; NSString *personTwo = @"Shawn"; NSString *combinedString = [NSString stringWithFormat: @"%@: Hello, %@!", personOne, personTwo]; NSLog(@"%@", combinedString); NSString *tipString = @"24.99"; float tipFloat = [tipString floatValue];

Defining Properties
! @property (attribute1, attribute2) type propertyName; strong weak assign copy nonatomic readwrite readonly !
Adds reference to keep object alive Object can disappear, become nil Normal assign, no reference Make copy on assign Make not threadsafe, increase perf Create getter&setter (default) Create just getter

Defining Methods

! - (type)doIt; - (type)doItWithA:(type)a; - (type)doItWithA:(type)a b:(type)b;

Implementing Methods
! - (type)doItWithA:(type)a b:(type)b { // Do something with a and b... return retVal; }

NSArray Quick Examples


! NSMutableArray *array = [@[person1, person2] mutableCopy]; [array addObject:@"Waldo"]; NSLog(@"%d items!", [array count]); for (NSString *person in array) { NSLog(@"Person: %@", person); } NSString *waldo = array[2];

Using Properties
[myObject setPropertyName:a]; myObject.propertyName = a; // alt a = [myObject propertyName]; a = myObject.propertyName; // alt

!Source: raywenderlich.com. Visit for more iOS resources and tutorials!

Version 1.5. Copyright 2013 Ray Wenderlich. All rights reserved.

You might also like