Skip to main content

Foundation Framework Classes with Examples

  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: 


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 string1
    string2 = [NSMutableString stringWithString: string1]; // Copy string1 object to string2
    [string2 appendString: @" and it is mine!"]; // Modify string2
NSLog (@"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:
    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);

Comparison:
    
    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:


  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)

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

Popular posts from this blog

UIKit Framework Hierarchy

UIKIT Framework Hierarchy

CoreData and its Interview Questions

Core Data takes advantage of the Objective-C language and its runtime, and neatly integrates with the Core Foundation framework. The result is an easy to use framework for managing an object graph that is elegant to use and incredibly efficient in terms of memory usage. Every component of the Core Data framework has a specific purpose and function. If you try to use Core Data in a way it wasn't designed for, you will inevitably end up struggling with the framework. Developers new to the Core Data framework often confuse it with and expect it to work as a database. If there's one thing I hope you'll remember from this series, it is that Core Data isn't a database and you shouldn't expect it to act like one. It's the Model in the Model-View-Controller pattern that permeates the iOS SDK. Core Data isn't the database of your application nor is it an API for persisting data to a database. Core Data is a framework that manages an object graph. It&

Middle Level iOS Developer interview questions

Middle Level iOS Developer interview questions 1) App Thinning 2) Steps for pushing application to app store 3) How push notification works 4) How google TEZ app getting near devices for get and send money 5)Retain Concept in iOS 6)Memory management  in iOS 7)ARC means 8) Syntax for shared instance 9) optionals and unwraping 10) Design pattarns 11) Tab bar insertion between screens 12) how to load images in table view using urls for each image by smooth scrolling  13) Extenstions 14)Clousers 15)Life cycles  16)how Table view reusable works?

Like us on Facebbok