NSString objects are usually created in one of three ways:
Using string literals like @”this example”
NSString* originalString = @"Text of the string";
NSString* uppercaseString = [originalString uppercaseString];
Loading strings from other data, like files
Generating strings from existing strings
NSString* startSubstring = [originalString substringToIndex:5]; // "This "
NSString* endSubstring = [originalString substringFromIndex:5]; // "is An EXAMPLE"
// make a range
NSRange theRange = NSMakeRange(2,5); // note: NSRange is not an Objective-C class, but rather a plain old C structure.
NSString* substring = [originalString substringWithRange:theRange]; // "is is"
if ([firstString isEqualToString:secondString]) {
// Do something
}
// note: isEqualToString is case-sensitive
SString* sourceString = @"Four score and seven years ago";
NSRange range = [sourceString rangeOfString:@"SEVEN"
options:NSCaseInsensitiveSearch];
An array is simply a list of objects. Arrays store a collection of objects in order, and allow you to refer to a specific item in the collection or all of them at once.
// create
NSArray* myArray = @[@"one", @"two", @"three"];
NSArray* myArray = @[@"one", @"two", @"three"];
int index = [myArray indexOfObject:@"two"]; // should be equal to 1
if (index == NSNotFound) {
NSLog(@"Couldn't find the object!");
}
NSArray* myArray = @[@"one", @"two", @"three"];
for (NSString* string in myArray) {
// this code is repeated 3 times, one for each item in the array
}
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
// item in the array at this point
// The array now contains just "Two"
// replace
[myArray replaceObjectAtIndex:1 withObject:@"Bananas"];
myArray[0] = @"Null";
NSDictionary* translationDictionary = @{
@"greeting": @"Hello",
@"farewell": @"Goodbye"
};
NSDictionary* translationDictionary = @{@"greeting": @"Hello"};
NSString* greeting = translationDictionary[@"greeting"];
// foreach
// Here, aDictionary is an NSDictionary
for (NSString* key in aDictionary) {
NSObject* theValue = aDictionary[key];
}
NSMutableDictionary* aDictionary = @{};
aDictionary[@"greeting"] = @"Hello";
aDictionary[@"farewell"] = @"Goodbye";
NSNumber* theNumber = @123;
int myValue = [theNumber intValue];
// 'numbers' is an NSMutableArray
[numbers addObject:theNumber];
int a=100;
NSNumber* number = @(a+1);
// Assuming that there is a text file at /Examples/Test.txt:
NSString* filePath = @"/Examples/Test.txt";
NSData* loadedData = [NSData dataWithContentsOfFile:filePath];
// Here, loadedData is an NSData object
NSString* filePath = @"/Examples/Test.txt";
[loadedData writeToFile:filePath atomically:YES];