Classes
Class header (MyClassName.h) #import "AnyHeaderFile.h" @interface MyClassName : SuperClassName // define public properties // define public methods @end |
|
Class implementation (MyClassName.m) #import "MyClassName.h" @interface MyClassName () // define private properties // define private methods @end @implementation MyClassName { // define private instance variables } // implement methods @end |
Variables
Declaring variables
type myVariableName;
|
|
Variable types int
|
Properties
Defining properties @property (attribute1, attribute2, ...) type myPropertyName; Automatically defines a private instance variable
type _myPropertyName;
Automatically creates a getter and setter - (type)myPropertyName; - (void)setMyPropertyName:(type)name; Using _myPropertyName uses the private instance variable directly.
|
|
Property attributes strong
|
Methods
Defining methods - (type)doIt; - (type)doItWithA:(type)a; - (type)doItWithA:(type)a andB:(type)b; |
|
Implementing methods - (type)doIt { // do something return ReturnValue; } - (type)doItWithA:(type)a { // do something with a return ReturnValue; } - (type)doItWithA:(type)a andB:(type)b { // do something with a and b return ReturnValue; } |
Constants
File specific constants static const double name = value; static NSString * const name = value; |
|
External constants // .h extern const double name; // .m const double name = value; // .h extern NSString * const name; // .m NSString * const name = value; |
Usage
Creating objects ClassName *myObject = [[ClassName alloc] init]; |
|
Using properties // setting value [myObject setMyPropertyName:a]; // or myObject.myPropertyName = a; // getting value a = [myObject myPropertyName]; // or a = myObject.myPropertyName; |
|
Calling methods [myObject doIt]; [myObject doItWithA:a]; [myObject doItWithA:a andB:b]; |
Example
Custom initializer - (id)initWithParam:(type)param { if ((self = [super init])) { _myPropertyName = param; } return self; } |
|
NSString NSString *personOne = @"Ray"; NSString *personTwo = @"Shawn"; NSString *combinedString = [NSString stringWithFormat:@"%@: Hello, %@!", personOne, personTwo]; NSLog(@"%@", combinedString); NSString *tipString = @"24.99"; float tipFloat = [tipString floatValue]; |
|
NSArray NSMutableArray *array = [@[person1, person2] mutableCopy]; [array addObject:@"Waldo"]; for (NSString *person in array) { NSLog(@"Person: %@", person); } NSString *waldo = array[2]; |
Notes
- Based on a cheat sheet by Ray Wenderlich