Saturday, January 26, 2013

COCO Foundation Framework in Objective C

This framework mainly consists of classes like NSArray , NSString etc.
Some classe starts with  CF they belongs to core foundation . Core  foundation is like COCO but implemented in c. Max of its code is open source. eg CFArray.
Classes starts with NS all are part of COCO.

NSValue
It used   to allow items of such data types to be added to collections such as instances of NSArray and NSSetIt can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object ids. 
Creation with
+ (NSValue *)value:(const void *)value withObjCType:(const char *)type
Ex: NSValue *value= [NSValue valueWithBytes: &rect objCType: @encode(NSRect)];
to retrive u can use [value getValue: &rect];

NSNumber
Its subclass of NSValue .Mainly for converting primitive data type into objective C object.
Creation with:-

+ (NSNumber *) numberWithChar: (char) value;
+ (NSNumber *) numberWithInt: (int) value;
+ (NSNumber *) numberWithFloat: (float) value;
+ (NSNumber *) numberWithBool: (BOOL) value;

NSPonit:-

typedef struct _NSPoint {
float x;
float y;
} NSPoint;

Initialization:-

1. NSPonit point = NSMakePoint (9, 18); // this is used when initializing NSPonit when passing its Object tot any function. without specifying object 
eg.[obj getStr: NSMakePoint (9, 18)];
2. NSPonit point = { 9, 18 };

 NSSize
typedef struct _NSSize {
float width;
float height;
} NSSize;

 NSRect
typedef struct _NSRect {
NSPoint origin;
NSSize size;
} NSRect;

same way of initialization as NSPoint NSMakePoint()NSMakeSize(), and NSMakeRect()

NSRange:-
typedef struct _NSRange {
unsigned int location;
unsigned int length;
} NSRange;

Mainly used for finding range. location pointing to starting point  and length specifies the length from location. Ex. used in string.
When location has NSNotFound means range does not refer to anything.
Initialization
same way of intialization as NSPoint  normal + NSMakeRange (9, 18);

NSString:-
NSString are immutable. Once created you can not change them. You can just access it.
Its class used for storing/creating/access string.
Some of common method used for string are
1. length :- for getting length of string
2.stringWithFormat: :-for creating  string with format specifier. mainly  for equality
eg[NSString stringWithFormat:@"I will be in office at %d am", 10];
3. isEqualToString: for comparison just return true or false depend upon match.
4.compare: does case sensitive comparison.with  character-by- character comparison.
return values are NSComparisonResult.

typedef enum _NSComparisonResult {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
} NSComparisonResult;

eg NSOrderedAscending when left- hand value is smaller than the right- hand one.
5. For case in sensitive compare use

- (NSComparisonResult) compare: (NSString *) string options: (unsigned) mask;
with mask value is NSCaseInsensitiveSearch
maks value will be NSNumericSearch] when strings are compared as number rather than characters.
6. when want to find first  substring with its location and range then use this- (NSRange) rangeOfString: (NSString *) aString;
7.- (BOOL) hasPrefix: (NSString *) aString;    for to check prefix value
- (BOOL) hasSuffix: (NSString *) aString;    for to check suffix value.

NSMutableString Its subclass of NSString
NSMutableString are mutable string.
You can manipulate string
1.add more character with    i.- (void) appendString: (NSString *) aString;
                                             ii.- (void) appendFormat: (NSString *) format, ...;
2. delete characters with i.- (void) deleteCharactersInRange: (NSRange) range;
3.creation + (id) stringWithCapacity: (unsigned) capacity;


NSArray
For storing only objective C objects. you can not store nil also.NSArray creates static arrays.
Static array means  you can not modifies its objects.

NSMutableArray
It is subclass of NSArray.  It is mutable array i.e. modifiable array of objects.
i. Add object at end - (void) addObject: (id) anObject;
ii. remove object  - (void) removeObjectAtIndex: (unsigned) index;

NSDictionary

It is used to store the key - value pair. It uses hashing mechanism so it is faster than array.

NSMutableDictionary
subclass of NSDictionary . mutable class.

NSNull 
We can not store the nil directly into NSArray or in NSDictionary.
NSNull used to store the nil value in NSArray and NSDictionary.
Mainly used to store  nothing . 
Example :- In case of keeping the employee info. Some employee may not have bike.

Fast Enumeration

It is used for to iterate through all objects of array. this is much faster.
ex
for (NSpoint *pt in array) {
NSLog (@"Value of point %@", pt);
}



NSDate

To get the date and time.


NSDate *date = [NSDate date];// date will be having current date and time.


NSData
It is used to hold buffer. It has buffer pointer plus length of buffer. 
It is mainly used to make memory management easy.
const char *string = "This is for test";

NSData *data = [NSData dataWithBytes: string length: strlen(string) + 1];



0 comments:

Post a Comment

About

Powered by Blogger.