Tuesday, June 1, 2010

Linker Issue" for class reference.

Hi all,

I have seen a lot of issues in different forums regarding "Linker Issue" for class reference.

Firstly try to show target in navigator and check whether any file is unchecked like in Fig 1

Fig 1

If there is any file being unchecked then it will show an error as in Fig 1.

".objc_class_name_SecondLinkerViewController", referenced from :

Literal-pointer@__OBJC@__cls_refs@SecondLinkerViewController in LinkerViewController.o
Symbol(s) not found
Collect2 : Id returned 1 exit


To resolve this issue, we are required to check the target checkmark icon, as in Fig 2



After checking these files, and build again, this issue will get resolved.

Thanks.


Friday, May 28, 2010

How to read binary data from file system

Good evening every one,

Today I have created one small code where I am trying to read and write the data in the binary format, so as to create the utility class for file management.

In this utility class, I am saving all the data in byte format, so that the end user if wants to write the encrypted data also, then he can encrypt and decrypt the data format at his end, and reading * writing of the data in file system should not get affected.

Here is the small example I am sharing regarding writing the binary data and reading it in collection format.

The utility class which I have created is named "FileManagerUtil.h,.m".

So in FileManagerUtil.h file I have created 5 methods,

-
(NSMutableData*) readDataForFile:(NSString*)fileName_;
- (BOOL) writeData:(id)data_ toFile:(NSString*)fileName_;


- (NSString *)removeTempDirectory;

- (NSString *)tempDirectoryPath:(NSString*)fileName_;

- (NSString *)applicationDocumentsDirectory;


and in FileManager.m file I have defined these methods as

1)

- (NSMutableData*) readDataForFile:(NSString*)fileName_ {
// check whether the document directory is available or not

NSString *documentsDirectoryPath = [self performSelector:@selector(tempDirectoryPath:) withObject:fileName_];


if ([[NSFileManager defaultManager] isReadableFileAtPath:documentsDirectoryPath]) {
NSMutableData *data_ = [NSMutableData dataWithContentsOfFile:documentsDirectoryPath];

return data_;
}

return nil;

}


2)

- (BOOL) writeData:(id)data_ toFile:(NSString*)fileName_ {
// check whether the document directory is available or not

NSString *documentsDirectoryPath = [self performSelector:@selector(tempDirectoryPath:) withObject:fileName_];
debug(@"%@",documentsDirectoryPath);

if ([[NSFileManager defaultManager] isWritableFileAtPath:documentsDirectoryPath]) {
NSLog(@"%@",data_);
[data_ writeToFile:documentsDirectoryPath atomically:YES];
return YES;
}

return NO;
}


3)

/**
Returns the path to the application's temp directory path.
*/

- (NSString *)removeTempDirectory {

NSString *tempDirPath = [[self performSelector:@selector(applicationDocumentsDirectory)] stringByAppendingPathComponent:@"temp"];

[[NSFileManager defaultManager] removeItemAtPath:tempDirPath error:nil];

return tempDirPath;
}



4)
/**
Returns the path to the application's temp directory path.
*/

- (NSString *)tempDirectoryPath:(NSString*)fileName_ {

NSString *tempDirPath = [[self performSelector:@selector(applicationDocumentsDirectory)] stringByAppendingPathComponent:@"temp"];

BOOL isDir = NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:tempDirPath isDirectory: &isDir]) {
[[NSFileManager defaultManager] createDirectoryAtPath:tempDirPath attributes: nil];
}
if (!isDir) {
[[NSFileManager defaultManager] removeItemAtPath:tempDirPath error:nil];
[[NSFileManager defaultManager] createDirectoryAtPath:tempDirPath attributes:nil];
}

NSString *tempFilePath = [tempDirPath stringByAppendingPathComponent:fileName_];

if(![[NSFileManager defaultManager] isWritableFileAtPath:tempFilePath]){
[[NSFileManager defaultManager] createFileAtPath:tempFilePath contents:nil attributes: nil];
}
return tempFilePath;
}


5)

/**
Returns the path to the application's documents directory.
*/

- (NSString *)applicationDocumentsDirectory {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}


Now, when I have to read the data from the file system for collections(like NSArray or NSDictionary),I am going to use NSPropertyListSerialization.

FileManagerUtil *fileManagerUtil = [[FileManagerUtil alloc] init];
NSMutableData *binaryData = [fileManagerUtil readDataForFile:@"filename.extension"];
NSString *error;

// If were to use mutabilityOption:NSPropertyListImmutable,
// we would get a run time error when trying to insert another volume

NSPropertyListFormat format;

id cacheData =[NSPropertyListSerialization propertyListFromData:binaryData
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&error];

Now check the type of cache data for collection

if([[cacheData class] isKindOfClass:[NSMutableDictionary class]]){
NSMutableDictionary *dictionary = (NSMutableDictionary*)cacheData;
}
else
if([[cacheData class] isKindOfClass:[NSMutableArray class]]){
NSMutableArray *dictionary = (NSMutableArray*)cacheData;
}



So in this way, if we want to secure our data in file system, we can do encryption of data before writing into file system and then decrypt it and then do its type parsing.

Thanks

For any quiries, please send me the mail, or put in the comments.