• 22nd, Jun 2010

Objective-C – Fundamentals – Handbook

//The Objective-C language is a simple computer language designed to enable sophisticated object-oriented programming.
//Objective-C extends the standard ANSI C language by providing syntax for defining classes, and methods, as well as other constructs that
//promote dynamic extension of classes.
.h //Header files.
.m //Source files. This is the typical extension used for source files and can contain both Objective-C and C code.
.mm //Source files. A source file with this extension can contain C++ code in addition to Objective-C and C code

//** Enum **
//declaration
typedef enum numberTypes
{
	ONE,
	TWO,
	THREE
} Numbers;
//use
Numbers x = TWO;
int num = (int)THREE;
//Null object is nil
NSMutableArray *myArray = nil;  // nil is essentially the same as NULL

/**
*
* Strings
*
*/
// Create a String.
NSString *myString;
NSString *myString2 = @"My String\n";
NSString *myString3 = nil;

// Concatenate numbers and other strings
NSString *anotherString = [NSString stringWithFormat:@"Number: %d and String: %s", 1, @"My String"];
//Double to String: %f 64-bit floating-point number (double)
//Char to String: %c
//int to string
[NSString stringWithFormat:@"%d", number];
//or
[[NSNumber numberWithInt:number] stringValue];

// Create an Objective-C string from a C string
NSString *fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding];

//String to Number
//String to Int
int x = [strNumber intValue];
//String to Float
float x = [strNumber floatValue];
//String to Double
double x = [strNumber doubleValue];
// NSData to String
unsigned char buffer[[txtEmail.text length]];
[txtEmail.text getBytes:buffer length:[txtEmail.text length]];
NSLog(@"Email: %s", buffer);
//Array to String
NSArray *cities  = [[NSArray alloc] initWithObjects:@"Barcelona", @"Madrid", @"Medellin", nil];
NSString *string = [cities componentsJoinedByString: @" / "];
NSLog(string); //Barcelona / Madrid / Medellin
//Date to String
NSLog([[NSDate date] descriptionWithCalendarFormat: @"%H:%M / %B %e, %Y" timeZone: nil locale: nil]);

//Remove Carriage Returns or replace any character
NSString *textarea = @"\n Text line1 \n Text line2\t more text\t \n Text line3\t\n  \n\n";
NSString *strOneLine = [textarea stringByReplacingOccurrencesOfString:@"\n" withString: @""];
NSLog(strOneLine);

//Trim Strings
NSString *textarea2 = @"\n Text line1 \t more text \t\n  \n\n";
NSString *trimstr = [textarea2 stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(trimstr);

/**
*
* Arrays
*
*/
// Create a new array .
NSArray *myArray = [[NSArray alloc] initWithObjects: @"Cat",@"Dog",@"Horse",@"Sheep", nil];

//Array of Arrays
NSMutableArray *searchFilters = [[NSMutableArray alloc] init];

// Initialize with Objects
NSArray *filters = [[NSArray alloc] initWithObjects: @"Cat",@"Dog",@"Horse",@"Sheep", nil];
[searchFilters addObject:filters];

filters = [[NSArray alloc] initWithObjects: @"White",@"Black",@"Red",@"Yellow", nil];
[searchFilters addObject:filters];

filters = [[NSArray alloc] initWithObjects: @"£0 - £20",@"£15 - £40",@"£40 - £80",@"+ £80",@"Any price", nil];
[searchFilters addObject:filters];

/**
*
* Dictionary / Hash Collections
*
*/
 //Create the dictionary
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
//Add data
[dictionary setObject:@"Toys" forKey:@"34"];
[dictionary setObject:@"Kitchen" forKey:@"45"];
[dictionary setObject:@"Electronics" forKey:@"46"];
//Get Data
NSLog([dictionary objectForKey:@"34"]);
//Get Keys ordered
NSArray* sortedKeys = [[myDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
//Get Values (Note: It will be unsorted)
NSArray* values = [myDict allValues];

/**
*
* Memory Management
*
*/
//Use of the objects if Garbage Collection is supported and activated (e.g.: iPhoneMac OS X supported)
NSString *str = [NSString string];

// Use of the objects if Garbage Collection is not supported (e.g.: iPhone)
MyObject *obj = [[MyObject alloc] init];
[...]
[obj release];

//Note: EXEC_BAD_ACCESS error usually happens when trying to access an object released.
// Only release objects that you retain, copy, alloc, or new.

/**
*
* Protocols and Delegates
*
*/
//A protocol declares methods that can be implemented by any class. Protocols are not classes themselves.
//They simply define an interface that other objects are responsible for implementing. When you implement the methods of a protocol in one of your classes,
//your class is said to conform to that protocol.
@protocol MyProtocol
- (void)myProtocolMethod;
@end

Tags: , , , , , , , , , , , ,

Leave a Reply

*

© 2010 unexpected[it]. All Rights Reserved.