Quantcast
Channel: iOS 6 – SDB Web Solutions
Viewing all articles
Browse latest Browse all 2

iOS 6 Basic Objective-C Course – part 2

$
0
0

SDB Web Solutions

iOS 6 Basic Objective-C Course - part 2

Declaring Variables in Objective-C

Variables are placeholders for data that you would like to hold in memory.

1. Follow camelCase convention. For example: simpleVariable, firstCourse, finalProduct, etc.

2. Variables shouldn't have no underline in their names.

3. Variables should contain only letters and numbers. Don't use punctuation, commas or dashes.

Types

1. NSInteger, store positive and negative integer values.

2. NSUInteger, store positive or zero integers.

3. float, store decimals

4. NSString, store strings, such as "Hello World!"

5. NSArray, store an array of objects like, (oranges, apples, pears)

6. NSSet, store unique instance of variables

 Allocating and Initializing strings

We have NSString and NSMutableString classes.

The NSString class is immutable, once it is created, its content cannot be modified.

NSMutableString is mutable, its content can be modified.

This is for instance an Objective-C string:

@"Hello World"

Other ways to represent strings inside an instance of NSString or NSMutableString classes:

NSString *newString = @"This is my simple string";

NSString *myString = [NSString stringWithString:@"Hi teacher!"];

NSString *firstString = [[NSString alloc] initWithString:@"Very well thanks"];

NSMutableString *theString = [[NSMutableString alloc] initWithString:@"Good afternoon!"];

Get the length of the string

NSString *companyName = [NSString alloc] init;

if ([ companyName length] == 0) {

// the person din not enter the company name

} else {

// the person input the company name

}

Convert a string to its integer value

Use integerValue, floatValue, and doubleValue.

NSString *aString = @"99.99";

NSInteger integerOfString = [aString integerValue];

NSLog(@"integerOfString = %ld", (long)integerOfString);

 

Find a string inside another string (needle inside haystack)

Use the rangeOfString: method. This search is case sensitive.

NSString *haystack = @"Dark side of the moon";

NSString *needle = @"moon";

if (range.location == NSNotFound){

// Could NOT find needle in haystack

} else {

// Found the needle in the haystack

NSLog(@"Found %@ in %@ at location %lu", needle, haystack, (unsigned long)range.location);

}

If you want now to search a string without sensitivity of the search, use NSCaseInsensitiveSearch method.

Comparing values in Objective-C with an If Statement

USe if statements.

NSInteger integer1 = 123;

NSInteger integer2 = 456;

if(integer1 ==integer2){

NSLog(@"Integers are equal.");

}else  {

NSLog(@"Integers are not equal");

}

Note: a double equal sign is used inside the conditional.

If you are comparing objects, it's best to use the isEqual: instance method of the NSObject class.

NSObject *object1 = [[NSObject alloc] init];

NSObject *object2 = object1;

if([object1 isEqual:object2]) {

NSLog(@"Both objects are equal.");

} else  {

NSLog(@"Objects are not equal.");

}

Of course, more complicated statements can be built using several "if then else" but for that we would need a 12 part tutorial and you can find more info on Google.

 

Go to iOS 6 Basic Objective-C Course part-3

Thanks for reading!

 

SDB Web Solutions - Develop Something New


Viewing all articles
Browse latest Browse all 2

Trending Articles