• No results found

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

N/A
N/A
Protected

Academic year: 2021

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

Copied!
1
0
0

Loading.... (view fulltext now)

Full text

(1)

 

Objective-C Cheat Sheet and Quick Reference

Copyright  2011  Ray  Wenderlich.    All  rights  reserved.  

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

Class Header (.h)

 

#import "AnyHeaderFile.h"

@interface ClassName : SuperClass { //declare instance variables }

// define properties

// define methods (including any // custom initializers)

@end

Class Implementation (.m)

 

#import "YourClassName.h"

@implementation ClassName // synthesize properties

// implement methods (including any // custom initializers, and dealloc)

@end

Defining Methods

 

- (anytype)doIt;

- (anytype)doItWithA:(anytype)a;

- (anytype)doItWithA:(anytype)a andB:(anytype)b;

Implementing Methods

 

- (anytype)doItWithA:(anytype)a andB:(anytype)b {

// Do something with a and b...

return retVal;

}  

Dealloc

- (void)dealloc {

// Release any retained variables...

[super dealloc];

}

Creating a Class Instance

 

ClassName * myClass =

[[ClassName alloc] init];

// myClass valid until you call:

[myClass release]; // Don’t need w/ ARC ClassName * myClass =

[[[ClassName alloc] init]

autorelease];

// myClass valid until next run loop

Calling a Method

 

[myClass doIt];

[myClass doItWithA:a];

[myClass doItWithA:a andB:b];

Defining Properties

 

@property (attribute1, attribute2) propertyName;

retain Call retain on assign assign Normal assign (default) copy Make copy on assign nonatomic Make not threadsafe

readwrite Create getter&setter (default) readonly Create just getter

 

Synthesizing Properties

 

@synthesize propertyName;

@synthesize propertyName = _myInstanceVariable;

Using Properties

[myClass setPropertyName:a];

myClass.propertyName = a; // alternative a = [myClass propertyName];

a = test.propertyName; // alternative

Declaring Variables

anytype myVariable;

int 1, 2, 500, 10000

float double

1.5, 3.14, 578.234

BOOL YES, NO, TRUE, FALSE ClassName * NSString *, NSArray *, etc.

id Can hold ref to any object  

Custom Initializer Example

 

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

}

return self;

}  

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];

NSArray Quick Examples

 

NSMutableArray *array =

[NSMutableArray arrayWithObjects:

personOne, personTwo, nil];

[array addObject:@"Waldo"];

NSLog(@"%d items!", array.count);

for (NSString *person in array) { NSLog(@"Person: %@", person);

}

NSString *waldo =

[array objectAtIndex:2];

References

Related documents

If we now remind ourselves of the introduction to this thesis, this analysis discusses the policy- making challenges of the sharing economy in relation to innovation and regulation.

Informanterna i undersökningen nämner också att det nästan inte finns någon av deras lärare som känner till begreppet (eller använder sig av det i sin egen undervisning)..

The Swedish House of Finance at the Stockholm School of Economics (SHoF) is accepting applications for a full-time pre-doctoral research analyst position..

näe sijaintisi kartalla ja katso miten pääset päämäärääsi Tervetuloa meille Raaseporiin. find places to eat, drink

Double click on the "Engine-1" part, then click on the Edit Object button and modify the attributes as shown below.. We have changed the Fuel Consumption Map Object

Flerbarnstillägg tillägg till barnbidraget eller studiebidraget till den som får barnbidrag, förlängt barnbidrag eller studiebidrag för två barn eller

ServiceMix > Home > Community > Team Download | Getting Started | Tutorials | Mailing Lists and Forums | Support | Source | Building.. OVERVIEW Home Download

ةكشر ّنإ Apple راعشو Apple ةكشرل ةيراجت تاملاع يه Apple Inc.، في ةل ّجسم رجتم .نادلبلا نم اهيرغو ةدحتلما تايلاولا App Store ةكشرل ةمدخ ةملاع وه نإ Android