Friday, February 1, 2013

File operation in objective C

You can use the standard c functions.
fopen(), fread(), fwrite(), open(), read() and write().


You can write objects of NSArray, NSDictionary, etc directly into file. There is having separate method ex writetoFile: method.

Example if u wrote array of string in Array and if u wrote that array into file. File will be having stuff like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>this is test 1</string>
<string>this is test 2</string>
<string>this is test 3</string>
</array>
</plist>

Some object have serialization and deserialization method which will write directly to file.

 An NSCoder  is an abstract class that defines a bunch of useful methods for converting your objects into an NSData  and back. So this is useful in serialization and de serialization. We are using  We’ll be NSKeyedArchiver  and NSKeyedUnarchiver classes which are subclass of  NSCoder.

NSKeyedArchiver is uses key/value pairs to hold an object’s information. It uses the name of the variable as key easy for decode also.

Some useful information

nib - it is called for .xib files (nib -  NeXT Interface Builder).  After compilation of xib we get the .nib file. .xib is a xml file.

Flow when nib files are used:-
When nib file loaded in to memory all the connection related to that nib files are set mean all IBoutlet get allocated and initialized. After the connection has been set awakeFromNib is sent to every object that was created.


 When the nib file was loaded into memory when the program ran, the objects were deserialized, and new NSWindow  and NSTextField  objects were created and hooked together

Protocols in Objective C

Protocols are like interface in Java. It is having all methods. The implementation of the methods of this protocols are implemented in other class in which they are derived/subclassed.

Syntax / Protocols are declared as follows:-


@protocol NSObserver
- (void) response: (NSString *) str;
@end

When class is derived from protocol then that class must be implement all the methods of that protocol.

Deriving / adopting protocol 

@interface NSHttpRequest : NSObject < NSObserver , NSRequestSend >
{
// instance variables
}
// methods
@end 

You can specify the protocol name in the datatype like declaration or return type.
Example:-
id< NSObserver > obj;

It is telling that obj will hold anytime of object which has adopted NSObserver protocol.

There are two different types of methods in protocol.
i.Optional
ii. Required

When class derive/ adopted protocol need not compulsory to implements optional methods.
Required methods are compulsory to implement.
Method without any directive are compulsory to implement.

Ex:.

@protocol NSHttpObserver
- (void) response: (NSString *) str;// compulsory to implement
@optional 
-(NSString*) getHeader; // optional to implement

@required
- (void)setUri(NSString*) uri;// compulsory to implement
- (void)setProxy(NSString*) proxy;// compulsory to implement


@end // BaseballPlayer









About

Powered by Blogger.