Strings
1.Creating Strings
NSString* aString = [[NSString alloc] init];
NSString* aString = @"Hello, world!";
NSInteger sizeOfString = [@"Hello, world!" length];
2.Capitalization and working with paths
NSString* originalString = @"This is An EXAMPLE";
// "THIS IS AN EXAMPLE"
NSString* upperCaseString = [originalString uppercaseString];
// "this is an example"
NSString* lowerCaseString = [originalString lowercaseString];
// "This Is An Example"
NSString* capitalizedString = [originalString capitalizedString];
3.Finding substrings
NSString* startSubstring = [originalString substringToIndex:5]; // "This "
NSString* endSubstring = [originalString substringFromIndex:5]; // "is An EXAMPLE"
NSRange theRange = NSMakeRange(2, 5);
NSString* substring = [originalString substringWithRange:theRange]; // "is is"
4.Comparing Strings
if(firstString == secondString) {
// Do something
}
if ([firstString isEqualToString:secondString]) {
// Do something
}
5.Searching Strings
NSString* sourceString = @"Four score and seven years ago";
NSRange range = [sourceString rangeOfString:@"seven"];
if (range.location == NSNotFound) {
// the string was not found
} else {
// the string was found; 'range' variable contains info on where it is
}
NSRange range = [sourceString rangeOfString:@"SEVEN" options:NSCaseInsensitiveSearch];
6.Arrays
NSArray* myArray = @[@"one, @"two", @"tree"];
NSString* oneString = myArray[0];
NSString* twoString = myArray[1];
NSString* oneString = [myArray objectAtIndex:0];
int count = myArray.count;
int index = [myArray indexOfObject:@"two"]; // should be equal to 1
if (index == NSNotFound) [
NSLog(@"Couldn't find the object!");
}
NSRange subArrayRange = NSMakeRange(1,2);
NSArray* subArray = [myArray subArrayWithRange:subArrayRange];
//subArray now contains "two", "three"
7.Fast Enumeration
for (NSString* string in myArray) {
// this code is repeated 3 times, one for each item in the array
}
8.Mutable Arrays
NSMutableArray* myArray = [NSMutableArray arrayWithArray:@[@"One", @"Two"]];
// Add "Three" to the end
[myArray addObject:@"Three"];
// Add "Zero" to the start
[myArray insertObject:@"Zero" atIndex:0];
// The array now contains "Zero", "One", "Two", "Three".
[myArray removeObject:@"One"]; // removes "One"
[myArray removeObjectAtIndex:1]; // removes "Three", the second
// The array now contains "Zero","Three"
[myArray replaceObjectAtIndex:1 withObject:@"One"]; // myArray is now "Zero", "One"
9.Dictionaries
NSDictionary* translationDictionary = @{
@"greeting": @"Hello",
@"farewell":@"Goodbye"
};
NSDictionary* translationDictionary = @{@"greeting" : @"Hello"};
NSString* greeting = translationDictionary[@"greeting"];
// Here, aDictionary is an NSDictionary
for (NSString* key in aDictionary) {
NSObject* theValue = aDictionary[key];
// do something with theValue
}
NSMutableDictionary* aDictionary = @{};
aDictionary[@"greeting"] = @"Hello";
aDictionary[@"farewell"] = @"Goodbye";
10.NSValue and NSNumber
NSNumber* theNumber = @123;
int myValue = [theNumber intValue];
int a = 100;
NSNumber* number = @(a+1); // 'number' contains 101