Foundation Framework is Foundation for making
iOS Applications
The Foundation framework defines a base layer of Objective-C classes. The Foundation framework is designed with
these goals in mind:
- Provide a small set of basic utility classes.
- Make software development easier by introducing consistent conventions for things such as deallocation.
- Support Unicode strings, object persistence, and object distribution.
- Provide a level of OS independence, to enhance portability.
The Foundation framework includes the root object class, classes
representing basic data types such as strings and byte arrays,
collection classes for storing other objects, classes representing
system information such as dates, and classes representing communication
ports.
"An immutable string is a text string that is defined when it is created and subsequently cannot be changed. An immutable string is implemented as an array of Unicode characters (in other words, a text string). To create and manage an immutable string, use the
NSString
class. To construct and manage a string that can be changed after it has been created, use NSMutableString
. "
Below are the mostly used Foundation Framework Classes grouped(Grouped: Based on the usage of that particular class):
To Handle Text and String:(NSString and NSMutableString)
NSString and NSMutable : Classes represent text strings and provide methods for searching, combining, and comparing strings.
Examples:
C language Example:
Mutable And Immutable:
Replace:
NSMutableString *string1 = [NSMutableString stringWithString: @"This is an Objective C Session"];
[string1 replaceCharactersInRange: NSMakeRange(8, 14) withString: @"a Swift"];
NSLog (@"string1 = %@", string1);
String Fetch Using Range:
Comparison:
Capitalisation:
Conversion:
NSArray, NSDictionary, NSSet: Provide storage for Objective-C objects of any classes, NSData: Provides object-oriented storage for arrays of bytes. NSValue and NSNumber: Provide object-oriented storage for arrays of simple C data values. Examples:
NSLog(@"Hi This is Awesome!");
NSString *sampleString = @"Hi This is Objective Session";
NSLog(@"sampleSting: %@ ",sampleString);
NSMutableString *sampleString1;
NSMutableString *sampleString2;
sampleString1 = [NSMutableString stringWithString: @"Hi this is string1 Content"];
sampleString2 = [sampleString1 mutableCopy];
[sampleString2 appendString: @" I just apended to String2"];
NSLog (@"string1 = %@", sampleString1);
NSLog (@"string2 = %@", sampleString2);
// 1. Creating an Array Object
NSArray *itemsList1;
itemsList1 = [NSArray arrayWithObjects: @"Pen", @"Pencil", @"Box", nil];
NSMutableArray *itemsList2;
itemsList2 = [NSMutableArray arrayWithObjects: @"Pen", @"Pencil", @"Box", nil];
NSMutableArray *itemsListArray = [NSMutableArray arrayWithObjects:itemsList1,itemsList2, nil];
NSLog(@"itemsMutalbleArray : %@", itemsListArray);
//Insertion
NSMutableArray *itemsList3;int i;
NSUInteger count;
itemsList3 = [NSMutableArray arrayWithObjects: @"Pen", @"Pencil", @"Box", nil];
[itemsList3 insertObject: @"Indigo" atIndex: 1];
[itemsList3 insertObject: @"Violet" atIndex: 3];
count = [itemsList3 count];
//Accessing
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [itemsList3 objectAtIndex: i]);
//or using FastEnumaration
for (NSString *arrayStringObject in itemsList3)
{
NSLog(@"arrayStringObject %@", arrayStringObject);
}
// Deleting:
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeObjectAtIndex: 0];
// To remove the first instance of a specific object from an array use removeObject:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeObject: @"Red"];
// To remove all instances of a specific object in an array, use removeObjectIdenticalTo:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", @"Red", @"Red", nil];
[myColors removeObjectIdenticalTo: @"Red"];
// To remove all objects from an array, use removeAllObjects:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeAllObjects];
// To remove the last object in the array, use the removeLastObject method:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeLastObject];
//7. Sorting:
NSMutableArray *colorsArray = [NSMutableArray arrayWithObjects: @"red", @"green", @"blue", @"yellow", nil];
NSArray *sortedArray;
sortedArray = [colorsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"sorted Array: %@",sortedArray);
// ******************** Dictionaries **********************
// ImmutableNature of Dictinary
NSDictionary *immutableItemListing = [NSDictionary dictionary];
immutableItemListing = [NSDictionary dictionaryWithObjectsAndKeys: @"iOS Developement", @"Course Name", @"Pavan ", @"Tutor", @"30 Days", @"Number of Days", @"Objective C", @"iOS Language", nil];
NSLog(@"immutableBookListing :%@",immutableItemListing);
//MutableNature of Dictionary
NSMutableDictionary *mutableItemsListing = [NSMutableDictionary dictionary];
// mutableItemsListing = [NSMutableDictionary dictionary];
[mutableItemsListing setObject: @"iOS Developement" forKey: @"Course Name"];
[mutableItemsListing setObject: @"Pavan" forKey: @"Tutor"];
[mutableItemsListing setObject: @"30 Days" forKey: @"Number of Days"];
[mutableItemsListing setObject: @"Objective C" forKey: @"iOS Language"];
[mutableItemsListing setObject: @"Swift" forKey:@"Progrmaing Language"];
NSLog(@"mutableBookListing :%@",mutableItemsListing);
// Count:
NSLog (@"Number of mutableBookListing = %lu", [mutableItemsListing count]);
//Accessing:
NSLog ( @"Course Name = %@", [mutableItemsListing objectForKey: @"Course Name"]);
NSLog ( @"iOS Language = %@", [mutableItemsListing objectForKey: @"iOS Language"]);
// Removing Particular Entry:
[mutableItemsListing removeObjectForKey: @"Number of Days"];
NSLog(@"mutableBookListing :%@",mutableItemsListing);
[mutableItemsListing removeAllObjects];
NSLog(@"mutableBookListing :%@",mutableItemsListing);
// NSFileManager, NSFileHandle and NSData
NSFileManager *iOSFileMgrSystem;
iOSFileMgrSystem = [NSFileManager defaultManager];
NSString *currentWorkingPath;
//Current Working Directory
iOSFileMgrSystem = [NSFileManager defaultManager];
currentWorkingPath = [iOSFileMgrSystem currentDirectoryPath];
NSLog (@"Current directory is %@", currentWorkingPath);
//Changing to a Different Directory
NSLog (@"Current directory is %@", currentWorkingPath);
if ([iOSFileMgrSystem changeCurrentDirectoryPath: @"/Users/Arepu/Desktop/"] == NO)
NSLog (@"Cannot change directory.");
currentWorkingPath = [iOSFileMgrSystem currentDirectoryPath];
NSLog (@"Current directory is %@", currentWorkingPath);
///Creating Directory
NSFileManager *iOSNewFileMgrSystem = [NSFileManager defaultManager];
NSURL *newDir = [NSURL fileURLWithPath:@"/Users/Arepu/Desktop/NewDictory/"];
[iOSNewFileMgrSystem createDirectoryAtURL: newDir withIntermediateDirectories:YES attributes: nil error:nil];
// Deleting Directory
[iOSNewFileMgrSystem removeItemAtPath:@"/Users/Arepu/Desktop/NewDictory/" error:nil];
˚˚
To Handle Text and String:(NSString and NSMutableString)
NSString and NSMutable : Classes represent text strings and provide methods for searching, combining, and comparing strings.
Examples:
C language Example:
char *myString = "This is a C character string";
other alternative using C Array:
char myString[] = "This is a C character array";
Objective C Language Examples:
Constant:
A constant string object is declared by encapsulating the string in double quotes (") preceded by an @ sign. For example:@"This is a constant character string object";
NSLog(@"%@",@"Hello this is constant");
Length:
NSString *string = @"Hello This is an Ojbective learning Session";int leng = (int)[string length];NSLog(@"length: %d",leng);
Mutable And Immutable:
NSString *immutableString = @"This is an example String to show immutable Nature";NSMutableString *mutableString = [NSMutableString stringWithString:@"This string is mutable"];Converting Immutable To Mutable:NSString *sample = @"This is a string"; NSMutableString *mutString; mutString = [NSMutableString stringWithString: sample]; String Copy:
NSMutableString *sampleString1;NSMutableString *sampleString2;sampleString1 = [NSMutableString stringWithString: @"Hi this is string1 Content"];sampleString2 = sampleString1;[sampleString2 appendString: @" I just apended to String2"]; NSLog (@"string1 = %@", sampleString1); NSLog (@"string2 = %@", sampleString2);How to create two independent NSString after copying:
NSMutableString *string1;NSMutableString *string2;string1 = [NSMutableString stringWithString: @"This is a string"]; // Initialize string1string2 = [NSMutableString stringWithString: string1]; // Copy string1 object to string2[string2 appendString: @" and it is mine!"]; // Modify string2NSLog (@"string1 = %@", string1); NSLog (@"string2 = %@", string2);
Range And SubString:
NSString *string1 = @"This is an Objective C Session!";NSRange match;
match = [string1 rangeOfString: @"Objective C"];NSLog (@"match found at index %lu", match.location); NSLog (@"match length = %lu", match.length);NSRange Macro: if (match.location == NSNotFound) NSLog (@"Match not found"); else NSLog (@"match found at index %lu", match.location);
Replace:
NSMutableString *string1 = [NSMutableString stringWithString: @"This is an Objective C Session"];
[string1 replaceCharactersInRange: NSMakeRange(8, 14) withString: @"a Swift"];
NSLog (@"string1 = %@", string1);
Search And Replace:
NSMutableString *string1 = [NSMutableString stringWithString: @"This is an Objective C Session"];
[string1 replaceCharactersInRange: [string1 rangeOfString: @"an Objective C"] withString: @"Swfit Programming!"]
NSLog(@"%@",string1);
Delete:
Delete:
NSMutableString *string1 = [NSMutableString stringWithString: @"This is an Objective C Session for Free "];
[string1 deleteCharactersInRange: [string1 rangeOfString: @"for Free "]];
NSLog(@"%@",string1);
String Fetch Using Range:
NSMutableString *string1 = [NSMutableString stringWithString: @"This is an Objective C Session"];
NSString *string2;
string2 = [string1 substringWithRange: NSMakeRange (11, 11)];
NSLog (@"string2 = %@", string2);
--------
string2 = [string1 substringFromIndex: 11];
NSLog (@"string2 = %@", string2);
Text Insertion:
NSMutableString *string1 = [NSMutableString stringWithString: @"This is an objective C Session"];
[string1 insertString: @"and also Swift " atIndex: 22];
NSLog (@"string2 = %@", string1);
NSString *string1 = @"String 1";
NSString *string2 = @"String 2";
if ([string1 isEqualToString: string2])
NSLog (@"Strings match");
else
NSLog (@"Strings do not match");
NSLog (@"string2 = %@", string1);
Prefix and Suffix:
Prefix and Suffix:
NSString *string1 = @"The is an Objective C Session";
BOOL result;
result = [string1 hasPrefix: @"The"];
if (result)
NSLog (@"String begins with The");
result = [string1 hasSuffix: @"Session"];
if (result)
NSLog (@"String ends with Session");
Capitalisation:
NSString *string1 = @"This is Objective C Session";
NSString *string2;
string2 = [string1 capitalizedString];
NSLog(@"String2: %@",string2);
NSString *string3 = [string1 lowercaseString];
NSLog(@"String3: %@",string3);
NSString *string4;
string4 = [string1 uppercaseString];
NSLog(@"String4: %@",string4);
Conversion:
NSString *string1 = @"10";
int myInt = [string1 intValue];
NSLog (@"%i", myInt);
How to maintain individual copies of objects after its assignment done: Copy
// Do any additional setup after loading the view, typically from a nib.
NSMutableString *string1 = [NSMutableString stringWithString:@"Text1"];
NSLog(@"String1 %@",string1);
NSMutableString *string2 = [NSMutableString stringWithString:@"Text2"];
NSLog(@"String2 %@",string2);
string1 = [string2 mutableCopy];
[string2 appendString:@"NewAppendedString"];
NSLog(@"String1 %@",string1);
NSLog(@"String2 %@",string2);
To Handle Application DataStorage: (NSArray, NSDictionary, NSData and its mutable Classes)
How to maintain individual copies of objects after its assignment done: Copy
// Do any additional setup after loading the view, typically from a nib.
NSMutableString *string1 = [NSMutableString stringWithString:@"Text1"];
NSLog(@"String1 %@",string1);
NSMutableString *string2 = [NSMutableString stringWithString:@"Text2"];
NSLog(@"String2 %@",string2);
string1 = [string2 mutableCopy];
[string2 appendString:@"NewAppendedString"];
NSLog(@"String1 %@",string1);
NSLog(@"String2 %@",string2);
To Handle Application DataStorage: (NSArray, NSDictionary, NSData and its mutable Classes)
NSArray, NSDictionary, NSSet: Provide storage for Objective-C objects of any classes, NSData: Provides object-oriented storage for arrays of bytes. NSValue and NSNumber: Provide object-oriented storage for arrays of simple C data values. Examples:
NSLog(@"Hi This is Awesome!");
NSString *sampleString = @"Hi This is Objective Session";
NSLog(@"sampleSting: %@ ",sampleString);
NSMutableString *sampleString1;
NSMutableString *sampleString2;
sampleString1 = [NSMutableString stringWithString: @"Hi this is string1 Content"];
sampleString2 = [sampleString1 mutableCopy];
[sampleString2 appendString: @" I just apended to String2"];
NSLog (@"string1 = %@", sampleString1);
NSLog (@"string2 = %@", sampleString2);
// 1. Creating an Array Object
NSArray *itemsList1;
itemsList1 = [NSArray arrayWithObjects: @"Pen", @"Pencil", @"Box", nil];
NSMutableArray *itemsList2;
itemsList2 = [NSMutableArray arrayWithObjects: @"Pen", @"Pencil", @"Box", nil];
NSMutableArray *itemsListArray = [NSMutableArray arrayWithObjects:itemsList1,itemsList2, nil];
NSLog(@"itemsMutalbleArray : %@", itemsListArray);
//Insertion
NSMutableArray *itemsList3;int i;
NSUInteger count;
itemsList3 = [NSMutableArray arrayWithObjects: @"Pen", @"Pencil", @"Box", nil];
[itemsList3 insertObject: @"Indigo" atIndex: 1];
[itemsList3 insertObject: @"Violet" atIndex: 3];
count = [itemsList3 count];
//Accessing
for (i = 0; i < count; i++)
NSLog (@"Element %i = %@", i, [itemsList3 objectAtIndex: i]);
//or using FastEnumaration
for (NSString *arrayStringObject in itemsList3)
{
NSLog(@"arrayStringObject %@", arrayStringObject);
}
// Deleting:
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeObjectAtIndex: 0];
// To remove the first instance of a specific object from an array use removeObject:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeObject: @"Red"];
// To remove all instances of a specific object in an array, use removeObjectIdenticalTo:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", @"Red", @"Red", nil];
[myColors removeObjectIdenticalTo: @"Red"];
// To remove all objects from an array, use removeAllObjects:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeAllObjects];
// To remove the last object in the array, use the removeLastObject method:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
[myColors removeLastObject];
//7. Sorting:
NSMutableArray *colorsArray = [NSMutableArray arrayWithObjects: @"red", @"green", @"blue", @"yellow", nil];
NSArray *sortedArray;
sortedArray = [colorsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"sorted Array: %@",sortedArray);
// ******************** Dictionaries **********************
// ImmutableNature of Dictinary
NSDictionary *immutableItemListing = [NSDictionary dictionary];
immutableItemListing = [NSDictionary dictionaryWithObjectsAndKeys: @"iOS Developement", @"Course Name", @"Pavan ", @"Tutor", @"30 Days", @"Number of Days", @"Objective C", @"iOS Language", nil];
NSLog(@"immutableBookListing :%@",immutableItemListing);
//MutableNature of Dictionary
NSMutableDictionary *mutableItemsListing = [NSMutableDictionary dictionary];
// mutableItemsListing = [NSMutableDictionary dictionary];
[mutableItemsListing setObject: @"iOS Developement" forKey: @"Course Name"];
[mutableItemsListing setObject: @"Pavan" forKey: @"Tutor"];
[mutableItemsListing setObject: @"30 Days" forKey: @"Number of Days"];
[mutableItemsListing setObject: @"Objective C" forKey: @"iOS Language"];
[mutableItemsListing setObject: @"Swift" forKey:@"Progrmaing Language"];
NSLog(@"mutableBookListing :%@",mutableItemsListing);
// Count:
NSLog (@"Number of mutableBookListing = %lu", [mutableItemsListing count]);
//Accessing:
NSLog ( @"Course Name = %@", [mutableItemsListing objectForKey: @"Course Name"]);
NSLog ( @"iOS Language = %@", [mutableItemsListing objectForKey: @"iOS Language"]);
// Removing Particular Entry:
[mutableItemsListing removeObjectForKey: @"Number of Days"];
NSLog(@"mutableBookListing :%@",mutableItemsListing);
[mutableItemsListing removeAllObjects];
NSLog(@"mutableBookListing :%@",mutableItemsListing);
// NSFileManager, NSFileHandle and NSData
NSFileManager *iOSFileMgrSystem;
iOSFileMgrSystem = [NSFileManager defaultManager];
NSString *currentWorkingPath;
//Current Working Directory
iOSFileMgrSystem = [NSFileManager defaultManager];
currentWorkingPath = [iOSFileMgrSystem currentDirectoryPath];
NSLog (@"Current directory is %@", currentWorkingPath);
//Changing to a Different Directory
NSLog (@"Current directory is %@", currentWorkingPath);
if ([iOSFileMgrSystem changeCurrentDirectoryPath: @"/Users/Arepu/Desktop/"] == NO)
NSLog (@"Cannot change directory.");
currentWorkingPath = [iOSFileMgrSystem currentDirectoryPath];
NSLog (@"Current directory is %@", currentWorkingPath);
///Creating Directory
NSFileManager *iOSNewFileMgrSystem = [NSFileManager defaultManager];
NSURL *newDir = [NSURL fileURLWithPath:@"/Users/Arepu/Desktop/NewDictory/"];
[iOSNewFileMgrSystem createDirectoryAtURL: newDir withIntermediateDirectories:YES attributes: nil error:nil];
// Deleting Directory
[iOSNewFileMgrSystem removeItemAtPath:@"/Users/Arepu/Desktop/NewDictory/" error:nil];
˚˚
Comments
Post a Comment
If any of my posts helped you.... or if you feel for any improvement.. you can leave a comment here.. So that, I will try to implement or make corrections for all of us.