Skip to content

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Ablfx-CodeBox Part2 |
//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)
//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
//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];
//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",
//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 *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
//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"];
@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];
//will call "encodeWithCoder" method in myClass.m
[NSKeyedArchiver archiveRootObject:myClassInstance toFile:@"/Users/blunckalex/save.plist"];
//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];
//header (.h):
@interface MyClass : NSObject {
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;}
//"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
for (int i=0; i<100; i++) {
//do stuff
if (i==50) {
break; //jumps out of loop here
//do stuff
for (int i=0; i<100; i++) {
//do stuff
if (i == 25) {
continue; //end this iteration - jump to top of loop
//do stuff
//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
//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];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.