public
Last active

Ablfx-CodeBox Part1 | ablfx.com

  • Download Gist
Objective-C - Class Object
1 2
Class == Blueprint
Object == Instance of class
Objective-C - Custom Initializers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
It is possible to use custon inits...e.g. to pass through arguments during allocation
 
//HEADER:
@interface MyClassName : NSObject { //NSObject Class name and Superclass
@private
int score; //private instance variables
}
-(id) initWithScore:(score*)s; //public methods
@end
 
 
 
 
//IMPLEMENTATION:
@implementation MyClassName
 
-(id) initWithScore:(int)s { //Every Object has an init method
self = [super init]; //"self" is the current object, [super init] the superclass
if (self) { //if object exists...
score = s;
}
return self;
}
 
-(id) init { //Every Object has an init method
self = [super init]; //"self" is the current object, [super init] the superclass
if (self) { //if object exists...
//code
}
return self;
}
 
@end
 
 
 
 
//Allocating in other Class:
MyClassName *myClassNameInstance = [[MycClassName alloc] initWithScore:500];
//...
[myClassNameInstance release];
Objective-C - Enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//define enumeration type with range of "variables"
enum playerState {idle, active, sad, angry};
 
//assign value to enum data type
enum playerState alexPlayerState = sad;
enum playerState maxPlayerState = idle;
 
//use as so:
if (alexPlayerstate == sad) {
//do stuff
}
 
 
//USING "typedef" to define your own data types:
typedef type name; //schematic
//define typedef enum:
typedef enum {idle, active, sad, angry} playerState;
//define your variables with own type:
playerstate alexPlayerstate = angry;
Objective-C - Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//Old C way --- USE METHODS
 
//function:
void myFunction () {
//do stuff
}
 
//to "trigger" function:
myFunction ();
 
//________________________________
//Pass variable to function:
NSString *message = @"Hello";
myFunction (message);
 
//function:
void myFunction (NSString *string) {
NSLog(@"the passed message is %@", string);
}
//Output will be "Hello"
Objective-C - Getters Setters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
Use this instead of global variables! Scroll down for @property!
 
//Getters and Setters are used to get and set private instance variables
//in oneclass from another class (without needing to use global variables)
 
//Hard-Coded Way:
 
//MyClass1.h
@interface: MyClass1 : NSObject {
@private
int score;
}
-(int) getScore;
-(void) setScore:(int)score;
@end
//MyClass1.m
@implementation
-(void) viewDidLoad {
score = 3;
}
-(int) getScore {
return score;
}
-(void) setScore:(int)s {
score = s;
}
 
 
//Now say in another class (MyClass2) you can get and set(change) the score in
//this class:
MyClass1 *myClass1Instance = [[MyClass1 alloc] init];
 
int integerName = [myClass1Instance getScore]; // the int value will be "3"
 
[myClass1Instance setScore:500]; //score in MyClass1 has now been set to 500
 
 
//USE PROPERTIES!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!
//In MyClass1.h remove both the setScore and getScore methods and add
//declare the private instance variable as a property (right before @end)
@property int score;
//in MyClass1.m "sysnthesize" this property:
@synthesize score;
//This will automatically create the getter/setter methods
//From another class you can now use this:
MyClass1 *myClass1Instance = [[MyClass1 alloc] init];
[myClass1Instance setScore:500]; //sets the score to 500
int myScore = [myClass1Instance score]; //myScore value is now 500
 
//dot Syntax is also possible (only with synthesized properties! :
int myScore = myClass1Instance.score; //getScore
myClass1Instance.score = 500; //setScore
Objective-C - Header Implementations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//HEADER:
@interface MyClassName : NSObject { //NSObject Class name and Superclass
@private
NSString *textMessage; //private instance variables
}
-(void) initWithText:(NSString*)text; //public methods
-(void) displayLog;
@end
 
 
 
 
//IMPLEMENTATION:
@implementation MyClassName
 
-(id) init { //Every Object has an init method
self = [super init]; //"self" is the current object, [super init] the superclass
if (self) { //if object exists...
//code
}
return self;
}
 
-(void) dealloc {
[super dealloc];
}
 
@end
 
 
//Allocating in other Class:
MyClassName *myClassNameInstance = [[MycClassName alloc] init];
//...
[myClassNameInstance release];
Objective-C - Methods Messages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//Simplest method without arguments:
-(void) myMethod {
//do stuff
}
 
//Call method with "message" :
[self myMethod];
//Call method from other class:
[className myMethod;];
 
 
//_________________________________________
//Method with arguments
-(void) myMethod: (int)age height:(float)height name:(NSString*)name  {
//do stuff with arguments
}
 
//Call said method with such message:
[self myMethod:21 height:1,78 name:@"Alex"];
 
 
//_________________________________________
//Nested method calls:
[self calculate2:[self calculate]];
 
//Methods:
-(int) calculate {
int result = 100 +200;
return result;
}
 
-(void) calculate2:(int)input {
int result = 300 + input;
NSLog(@"calculation result: %i", result);
}
//600 will be the result
 
 
//_________________________________________
//Difference between Class and Instance Methods:
 
//Class Methods:
+ // Works on the whole class [NSString stringWithFormat:@"hello"];
 
//Instance Methods:
- // Works on only an instance of a class [myString uppercaseString];
Objective-C - Operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//typical arithmetic operators:
+, -, *, /, =
 
//...combined with equal:
+=, -=, *=, /= // " result += a " same as " result = result+a "
 
 
//Comparison operators:
== // equal to
!= // not equal to
<
>
<=
>=
 
 
//Logical operators:
&& // logical and
|| // logical or
 
 
//Modulus operator (calculate remainder)
%
//Example:
int year = 2003;
int remainder = year % 3; //remainder = 4
 
 
//Ternary Operator:
condition ? True : False //Schematic
//Example (choose higher int value to add to int highScore):
int score1;
int score2;
 
int highestScore = (score1 > score2) ? score1 : score2;
 
 
//Increment / Decrement operators:
a++; // postfix a + 1 but doesn't change the value of a
++a; // prefix a + 1 changes value of a
a--; // postfix a - 1 but doesn't change the value of a
--a; // prefix a - 1 changes value of a;
 
 
//Cast operators
//convert typ into other type (during operation)
int a = 25;
int b = 2;
float result = (float) a / b;
Objective-C - Preprocessor Directives
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//import stuff:
#import
 
 
//replace stuff in code:
#define GRAVITY 9.97
//everywhere in code were "GRAVITY" appears it will be replaced with "9.97"
//example:
int result = GRAVITY *10;
 
 
//Conditional code:
//condition can be a number of things!
#if DEBUG
//do stuff
#endif
Objective-C - Switch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
int integerName = 345;
 
switch (integerName) {
case 340:
NSLog(@"integerName is 345");
break;
case 341:
NSLog(@"integerName is 345");
break;
case 342:
//...
case 343:
//...
case 344:
//...
case 345:
//...
default:
//gets called if none of the "cases" "match"
break;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.