public
Created

Ablfx-CodeBox Part2 | ablfx.com

  • Download Gist
Objective-C - Array
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
//Declaring NSArray:
//NSDate *myDate = [[NSDate alloc] init];//all objects cann be added to an array
NSArray *myArray = [NSArray arrayWithObjects: @"one", @"two", myDate, nil];
//Access objects in array:
[myArray objectAtIndex: 1]; //is String @"two"
 
//This type of Array is immutable (unchangeable, can't add/remove objects
//after creation)
 
 
//NSMutableArray
//NSDate *myDate = [[NSDate alloc] init];//all objects cann be added to an array
NSMutableArray *myArray = [NSMutableArray arrayWithObjects: @"one", @"two", myDate, nil];
//Access objects in array:
[myArray objectAtIndex: 1]; //is String @"two"
//Adding stuff after creation:
[myArray addObject: @"four"];
 
//remove stuff after creation:
[myArray removeObjectAtIndex:1];
//will remove string @"two" and collapse the array
//"myDate" is now at index 1
 
//get array size:
[myArray count]; //integer
 
 
//Readout the whole array with fast enumeration
//see "Dictionary" for application
Objective-C - Data Types
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
//Typical data types:
 
int // 123, -123
// Placeholder: %i
unsigned int // no negative numbers
long int // more than 2 billion numbers (64bit) for 32bit "long long int"
float // 1.23f
// Placeholder: %f
double // 1.23.....f double size
// Placeholder: %f
char // a
BOOL // YES / NO (use this!)
bool // true / false
 
id //any possible Object
 
//Class data types: (objects)
 
NSString *stringName // @"name"
// Placeholder: @% (placeholder for all objects
 
//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;
 
 
//Wrap an int into an object (NSNumber)
int myINT = 3;
NSNumber *myNumber = [NSNumber numberWithInt:myINT];
Objective-C - Dictionary
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
//Use this if you want an array like object to store objects which can be linked with keys to access them
 
//Creating a NSDictionary
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"myObject1", @"myKey1",
@"myObject2", @"myKey2",
@"myObject3", @"myKey3",
@"myObject4", @"myKey4",
@"myObject5", @"myKey5",
nil];
//Accessing objects by values
NSString *keyString = @"myKey3";
[myDictionary objectForKey:keyString]
//e.g. :
NSLog(@"the object for saif key is: %@", keyString); //"myObject3"
 
 
//like the NSArray NSDictionary is immutable (can't be changed after creation
//NSMutableDictionary:
NSMutableDictionary *myDictionary = [NSMutableDictionary ...];
//add Objects with corresponding values:
[myDictionary setObject:@"myObject6" forKey:@"myKey6"];
//remove Objects:
[myDictionary removeObjectForKey:@"myKey4"];
 
 
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Populating NSDictionary from .plist
//Create .plist file with Objects(Values) and corresponding Keys
NSString *path=[[NSBundle mainBundle] pathForResource:@"dictinary" ofType:@"plist"];
NSDictionary *myDictionary = [NSDictionary dictionaryWithContentsOfFile:path];
 
 
//Using "Fast Enumeration" to read out all Objects in Dictionary
for (NSString *keyString in myDictionary) {
NSLog(@"key:%@ object:%@",keyString ,[myDictionary objectForKey:keyString]);
}
//This will keep you from using standard "for" loop and needing to name keys
//with advancing numbers
Objective-C - File Management
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
//NSFileManager is used for many basic file management operations:
NSFileManager *fileMGR = [[NSFileManager alloc]init];
 
//copy/move a file in the filesystem:
[fileMGR copyItemAtPath:path toPath:@"/Users/blunckalex/mytext.rtf" error:nil];
[fileMGR moveItemAtPath:path toPath:@"/Users/blunckalex/mytext.rtf" error:nil];
 
//read out attributes of specific file:
NSLog(@"%@", [fileMGR attributesOfItemAtPath:path error:nil]);
 
 
//______________________________________________________________________________________
//Saving/Loading objects into a .plist file (Archiving/Unarchiving) (Encoding/Decoding):
//Saving a NSString object into a .plist file
NSString *myString = @"this is my string";
[NSKeyedArchiver archiveRootObject:myString toFile:@"/Users/blunckalex/myString.plist"];
 
//loading archived object from the .plist file into a new NSString
NSString *newString = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/blunckalex/myString.plist"];
 
//this can be done with any kind of object, also numbers (NSNumber)
//see "data types" for creation from int
 
 
//______________________________________________________________________________________
//Saving loading of more than 1 object from a custom made subclass
//See myClass.m, myClass.h in this entry how to prepare class
//See myMainClass.m to see "load"/"save" calls to myClass
 
 
 
//______________________________________________________________________________________
//Use NSURL when possible! (Faster than NSString when needing file paths)
NSURL *myURL = [NSURL fileURLWithPath:@"/Users/blunckalex/hello.txt"];
 
 
//MainClass.m:
@implementation MyMainClass
 
-(id) init { self = [super init]; if (self) {
MyClass *myClassInstance = [[MyClass alloc] init]; //allocate our object
[myClassInstance setMyString:@"this is text"]; //set the number/string (see Getters/Setters)
[myClassInstance setMyNumber:3];
//Save:
//will call "encodeWithCoder" method in myClass.m
[NSKeyedArchiver archiveRootObject:myClassInstance toFile:@"/Users/blunckalex/save.plist"];
//Load:
//Either load into new instance of our MyClass Class:
MyClass *anotherInstance = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/blunckalex/myString.plist"];
}return self;}
 
 
-(void) dealloc {
[myClassInstance release]; //if you alloc something / release it when done ;)
[super dealloc];
}
 
@end
 
 
//MyClass.h/.m:
//header (.h):
@interface MyClass : NSObject {
@private
NSString *myString;
int myNumber;
}
@property (retain) NSString *myString;
@property int myNumber;
 
 
//Implementation (.m):
@implementation MyClass
@synthesize myString, myNumber;
 
//set up method to be called when to save
//use exactly this method name!!
-(void) encodeWithCoder: (NSCoder *) coder {
[coder encodeObject:myString forKey:@"keyString"]; //Choose whatever key you like
//Since NSCoder expects an object wrap the int into a NSNumber object
NSNumber *myNumberWrapper = [NSNumber numberWithInt:myNumber];
[coder encodeObject:myNumberWrapper forKey:@"keyNumber"];
}
 
//set up method to be called when to load:
-(id) initWithCoder: (NSCoder *) coder {
myString = [coder decodeObjectForKey:@"keyString"];
[myString retain]; //retain so the object won't disappear (autorelease)
//load myNumber into NSNumber since it was saved that way
NSNumber *myNumberWrapper = [coder decodeObjectForKey:@"keyNumber"];
//use cast operator (see data types) to "load" NSNumber value back into myNumber int
myNumber = (int) [myNumberWrapper integerValue];
return self;
}
 
-(id) init { self = [super init]; if (self) {
}return self;}
 
@end
Objective-C - Loops
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 54 55 56 57 58 59 60
//"if" loop
if (condition) {
//execute me
}
 
 
//"while" loop
//while condition is valid execute code
while (condition) {
//execute me
}
 
 
//"do-while" loop
//same as "while" loop, but runs the code/loop once
do {
//execute me
} while (condition);
 
 
 
//_______________________________________
//"for" loop
//runs the code until condition is no longer met (example for condition: "i<10")
for (int i=0; condition; i++) {
//execute me
}
 
//_______________________________________
//Fast Enumeration
for (NSString *string in myDictionary) { //inherit "string" with strings out of collection
//do stuff
//see "Dictionary" for application
}
 
 
//_______________________________________
//JUMP OUT OF LOOP
break;
//example
for (int i=0; i<100; i++) {
//do stuff
if (i==50) {
break; //jumps out of loop here
}
//do stuff
}
 
 
//_______________________________________
//JUMP BACK UP TO TOP OF LOOP
continue;
//example
for (int i=0; i<100; i++) {
//do stuff
if (i == 25) {
continue; //end this iteration - jump to top of loop
}
//do stuff
}
Objective-C - Memory Management
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//What you own you must release
//If you don't own it you don't need to release it
 
//Memory Management is only to be used on objects
 
//Alloction and Inititization:
NSString *myString = [[NSString alloc] init];
 
//release of object after use:
[myString release];
 
//clear out pointer address:
myString = nil
 
//rlease allocated objects in the dealloc method:
-(void) dealloc {
[myString release]; //release the allocated object
[super dealloc]; //deallocate the superclass
}
Objective-C - NSString
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
//Basic Usage:
NSString *myString = @"this is my string";
 
 
//Adding variables (int, float, etc.) into a string:
int *myINT = 21;
NSString *myString = [NSString stringWithFormat:@"I am %i years old", myINT];
 
 
//NSString is immutable, inorder to change/add text to it use NSMutableString:
NSMutableString *myString = [NSMutableString stringWithFormat:@"this is my string"];
//Add text to existing NSString:
[myString appendString:@" with more text"];
//myString's value is now "this is my string with more text"
 
 
//"Populating" NSString with contents of a .txt file:
NSURL *myURL = [NSURL fileURLWithPath:@"/Users/blunckalex/hello.txt"];
NSString *myString = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:nil];
 
//Saving a NSString into a .txt file
[string1 writeToURL:myURL atomically:YES encoding:NSUTF8StringEncoding error:nil];

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.