-1

I'm using google drive files in my application. Accessing files, downloading files, uploading files are working as a charm. Now I'm trying to save the GTLDriveFile information on local(pList) by using NSCoding. Can you please help me to save this information into pList or suggest the best way to store this info in local

Steps followed:

  1. Google Authentication - done
  2. Querying Files - done
  3. Trying to save all file information into pList - I'm here now

Please refer this link for GTLDriveFile Class Structure https://developers.google.com/drive/ios/reference/ios-client/interface_g_t_l_drive_file

I've tried by this way, but i couldn't save the entire structure into it.

For Ex: .h file

#import "GTLDriveFile.h"

@interface sample : GTLDriveFile <NSCoding>

@end

.m file

#import "sample.h"

@implementation sample

//@synthesize File;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init])    {
         super.fileSize=[aDecoder decodeObjectForKey:@"fileSize"];
         super.originalFilename=[aDecoder decodeObjectForKey:@"originalFilename"];
         super.mimeType=[aDecoder decodeObjectForKey:@"mimeType"];
         super.title=[aDecoder decodeObjectForKey:@"title"];
         **super.parents=[aDecoder decodeObjectForKey:@"parents"];**
         super.lastModifyingUserName=[aDecoder decodeObjectForKey:@"lastModifyingUserName"];
         **super.copyable=[aDecoder decodeObjectForKey:@"copyable"];**
         super.kind=[aDecoder decodeObjectForKey:@"kind"];
         super.writersCanShare=[aDecoder decodeObjectForKey:@"writersCanShare"];
         **super.appDataContents=[aDecoder decodeObjectForKey:@"appDataContents"];
         super.modifiedDate=[aDecoder decodeObjectForKey:@"modifiedDate"];**
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject: super.fileSize forKey:@"fileSize"];
    [encoder encodeObject: super.originalFilename forKey:@"originalFilename"];
    [encoder encodeObject: super.mimeType forKey:@"mimeType"];
    [encoder encodeObject: super.title forKey:@"title"];
    **[encoder encodeObject: super.parents forKey:@"parents"];**
    [encoder encodeObject: super.lastModifyingUserName forKey:@"lastModifyingUserName"];
    **[encoder encodeObject:copyable forKey:@"copyable"];**
    [encoder encodeObject: super.kind forKey:@"kind"];
    [encoder encodeObject: super.writersCanShare forKey:@"writersCanShare"];
    **[encoder encodeObject:appDataContents forKey:@"appDataContents"];**
    **[encoder encodeObject: super.modifiedDate forKey:@"modifiedDate"];**
}

@end

Problem was, i couldn't serialize/de-serialize the direct values like NSString, NSNUMBER, and etc. When I'm trying on some base object like "copyable", I'm getting an exception(please refer the bolded objects or refer the GTLDriveFile class reference from the above link).

Guys, please help me to save this info to local and suggest the best way to do this.

Thanks

iTag
  • 409
  • 3
  • 19
  • Not clear what your question is. What are you trying to encode? Show that code. None of the code you show refers to `NSCoding` so what do you expect to implement it? – Wain Apr 25 '14 at 13:46
  • Sorry. and I followed initwithCoder and encodewithcoder mechanism.. its working fine for NSString and not working for NSArray @Wain – iTag Apr 25 '14 at 13:53
  • - (id)initWithCoder:(NSCoder *)aDecoder { if(self = [super init]) { name=[aDecoder decodeObjectForKey:@"name"]; array = [aDecoder decodeObjectForKey:@"array"]; } return self; } - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:name forKey:@"name"]; [encoder encodeObject:nsarray forKey:@"nsarray"]; } but How am i Encode and decode other class Object.? @Wain – iTag Apr 25 '14 at 13:59
  • 3
    Please [edit] your encoding and decoding methods into your question - the formatting is lost in comments. – thegrinner Apr 25 '14 at 14:08
  • Is that your actual code? You are using different key and variable names in each method, so I doubt it. – trojanfoe Apr 25 '14 at 15:13

2 Answers2

1

So Cocoa framework classes support NSCoding. Of these classes, some are 'plain' classes (like NSString) and they support everything that's required. Some are 'complex' classes (like NSArray) which contain instances of other classes - in this case, all of the contained objects must support NSCoding for the encoding to work properly.

Any non-framework class that you want to encode / decode, you need to specify that it conforms to NSCoding and you need to write an implementation of the protocol methods (calling super if appropriate).

Wain
  • 118,658
  • 15
  • 128
  • 151
1

In one of your comments, above, you effectively suggested the following NSCoder methods (clearly assuming you're dealing with an object with only two properties, name and array):

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super init])) {
        self.name  = [aDecoder decodeObjectForKey:@"name"];
        self.array = [aDecoder decodeObjectForKey:@"array"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.name  forKey:@"name"];
    [encoder encodeObject:self.array forKey:@"array"]; 
}

That's basically right if the super class does not, itself, conform to NSCoder. But if super conforms to NSCoder, you have to give it a chance to do its magic, too:

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        self.name  = [aDecoder decodeObjectForKey:@"name"];
        self.array = [aDecoder decodeObjectForKey:@"array"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [super encodeWithCoder:encoder];

    [encoder encodeObject:self.name  forKey:@"name"];
    [encoder encodeObject:self.array forKey:@"array"];
}

If you're subclassing some custom object (e.g. whether GTLObject or one of your own classes), it's critical to use that this also conforms to NSCoder. And you have to repeat this above pattern in all of your classes that will be included in the archive. But the Cocoa classes (like NSArray) already conform to NSCoder, so you only have to worry about your own classes.

Rob
  • 415,655
  • 72
  • 787
  • 1,044