Tuesday, March 1, 2011

Objective-C Basics

Hi Friends,


I'm trying to learn some Objective-c. Here's what I've got so far.


Creating dictionaries


JavaScript Version:
var jamund = {
    pic: "http://a0.twimg.com/profile_images/1164437235/IMG_0803.jpg",
    name: "Jamund Ferguson",
    position: "Sr. JavaScript Developer",
    yearsEmployed: "1",
    twtiter: "xjamundx",
    email: "jamund@gmail.com"
};


Objective-C Version:

NSDictionary *jamund = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"http://a0.twimg.com/profile_images/1164437235/IMG_0803.jpg", @"pic",
                        @"Jamund Ferguson", @"name",
                        @"Sr. JavaScript Developer", @"position",
                        @"1", @"yearsEmployed",
                        @"xjamund", @"twitter",
                        @"jamund@gmail.com", @"email", nil];


Splitting a string into an array of parts


JavaScript Version:
var myString = "This is a test";
var myWords = myString.split(" "); // ["this", "is", "a", "test"]
console.log("First word: " + myWords[0]); // First word: This








Objective-C Version:








NSString *myString = @"This is a test";
NSArray *myWords = [myString componentsSeparatedByString:@" "];
NSLog(@"First word: %@", [myWords objectAtIndex:0]); // First word: This


Getting the first name in a full name string



JavaScript Version:

var name = "Jamund Ferguson";
console.log("Location: " + name.indexOf(" ")); // Location: 6
var firstName = name.slice(0, name.indexOf(" ")); // Jamund


Objective-C Version:
NSString *name = @"Jamund Ferguson";
NSRange r = [name rangeOfString:@" "]; 
NSLog(@"Location: %i", r.location); // Location: 6
NSLog(@"Range: %@", NSStringFromRange(r));
NSLog(@"First name: %@", [name substringToIndex:r.location]);// First name: Jamund


http://stackoverflow.com/questions/256460/nsstring-indexof-in-objective-c


Creating a Simple Array of Strings


Objective-C Version:

NSArray *friends = [NSArray arrayWithObjects: @"Cat", @"Dog", @"Mouse", @"House", @"Pals", @"Rob", nil];



JavaScript Version:
var friends = ["Cat", "Dog", "Mouse", "House", "Pals", "Rob"];




WTFs!?

"Missing sentinel in function call" -  Just means that you must end your array with a nil (see above).


Adding the same subview to a view multiple times in row leads to it only being added once:





int j = [[self.view subviews] count];
NSLog(@"You have %i subviews loaded", j); // You have 2 subviews loaded
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
j = [[self.view subviews] count];
NSLog(@"You have %i subviews loaded", j); // You have 3 subviews loaded

This means that we don't have to worry about checking if a view has been added before adding it again, which is actually pretty convenient!

Resources


A great tutorial:
http://cocoadevcentral.com/d/learn_objectivec/


Another tutorial:
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/

No comments:

Post a Comment