开发者

Unable to write NSArray to file

开发者 https://www.devze.com 2023-04-12 20:05 出处:网络
I am using google contact data objective c APIs for fetching contacts. I got contacts array from google server now i want to write contact to file. i am using writeToFile:atomically: method for writin

I am using google contact data objective c APIs for fetching contacts. I got contacts array from google server now i want to write contact to file. i am using writeToFile:atomically: method for writing array to file but This method is not working for me since i feel that output array from 开发者_运维技巧gdata API not contain property list objects. Please suggest any alternate solution.

-(void)fetchData{
    GDataServiceGoogleContact *service=[[GDataServiceGoogleContact alloc] init];
    [service setShouldCacheResponseData:YES];
    [service setServiceShouldFollowNextLinks:YES];
   [service setUserCredentialsWithUsername:[mUsername stringValue] password:[mPassword stringValue]];

    // GENERATING THE URL
    NSURL *feedURL=[GDataServiceGoogleContact contactFeedURLForUserID:kGDataServiceDefaultUser];
    GDataQuery *contQuery=[GDataQueryContact contactQueryWithFeedURL:feedURL];
    [contQuery setShouldShowDeleted:YES];
    [contQuery setMaxResults:2000];
    GDataServiceTicket *ticket=[service fetchFeedWithQuery:contQuery delegate:self didFinishSelector:@selector(hasFetchedContacts:feed:error:)];    
}


-(void) hasFetchedContacts:(GDataServiceTicket*) ticket  feed:(GDataFeedContact*) contacts error:(NSError*) err
{
    NSArray *contactList=[contacts entries];
    NSLog(@"%d",[list writeToFile:@"/Users/subhranil/Desktop/contactList" atomically:NO]);  

}


Wrap it up to NSData with:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:contactList];

Then save NSData to file with:

[data writeToFile:@"/Users/subhranil/Desktop/contactList" atomically:NO];

You can later restore the data back to NSArray using:

NSData *data = [NSData dataWithContentsOfFile: @"yourFilePath"];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data]

Just make sure that objects inside your NSArray conform to NSCoding.


You can use byte array for this purpose and NSData for writing to file.
For saving:

NSData *data=[[NSData alloc] initWithBytes:[contacts entries] length:total];
[data writeToFile:@"path" atomically:YES];

total= The total size of the array in bytes

For retrieving:

   NSData *newdata = [NSData dataWithContentsOfFile:@"path"];
   NSUInteger len = [newdata length];
   Byte *byteData = (Byte*)malloc(len);
   memcpy(byteData, [newdata bytes], len);

byteData will now contain an array of GDataEntryContact objects and you can use them accordingly.


You can encode/decode GDataObject using an xml as generator.

Encode:

[entry setNamespaces:[entry completeNamespaces]];
NSString *xml = [[entry XMLElement] XMLString];
if (nil != xml) 
{
    //Store your xml NSString to a file
}

Decode:

NSString *xml = //Read your XML String from file;
NSXMLElement *xmlElement = [[NSXMLElement alloc] initWithXMLString:xml error: &error];
if (!error) {
    return [[GDataEntryDocBase alloc] initWithXMLElement:xmlElement parent: nil];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消