Pregunta

I am working on an iOS App that involves saving and retrieving an NSMutableArray containing multiple instances of a single custom object I made. I have seen several guides such as Apple's Documentation

I get the gist of how to do it (I think), it seems I have to use archiving since my objects in the array are not primitive variables, therefore I have already made my objects NSCoding compliant. However I have also seen examples using NSDefaults or whatever that I don't understand (I have no file IO experience). After seeing all of this info, I am having a particularly hard time piecing everything together. What am looking for is a complete guide, from start to finish, of an example program that successfully uses archiving to save and retrieve custom objects (in an array or not). If someone could point out a good guide to me or make their own on this post, that would be GREATLY appreciated! Thanks everyone, Stack Overflow is an awesome place!

P.S. If more information is needed please tell me in the comments!

¿Fue útil?

Solución

Make sure that any class you are trying to archive implements the NSCoding protocol, then do something like this:

@interface MyClass<NSCoding>

@property(strong,nonatomic) NSString *myProperty;

@end

@implementation MyClass
#define myPropertyKey @"myKey"
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if( self != nil )
    {
        self.myProperty = [aDecoder decodeObjectForKey:myPropertyKey];
    }

    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:[self.myProperty copy] forKey:myPropertyKey];

}

@end

Then I use a class called FileUtils to do my archiving work:

@implementation FileUtils


+ (NSObject *)readArchiveFile:(NSString *)inFileName
{
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName];


    NSObject *returnObject = nil;
    if( [fileMgr fileExistsAtPath:filePath] )
    {
        @try
        {
            returnObject = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        }
        @catch (NSException *exception)
        {
            returnObject = nil;
        }
    }

    return returnObject;

}

+ (void)archiveFile:(NSString *)inFileName inObject:(NSObject *)inObject
{
    NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName];
    @try
    {
        BOOL didSucceed = [NSKeyedArchiver archiveRootObject:inObject toFile:filePath];
        if( !didSucceed )
        {
            NSLog(@"File %@ write operation %@", inFileName, didSucceed ? @"success" : @"error" );
        }
    }
    @catch (NSException *exception)
    {
        NSLog(@"File %@ write operation threw an exception:%@", filePath,     exception.reason);
    }

}

+ (void)deleteFile:(NSString *)inFileName
{
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName];
    NSError *error;
    if ( [fileMgr fileExistsAtPath:filePath] && [fileMgr removeItemAtPath:filePath error:&error] != YES)
    {
        NSLog(@"Unable to delete file: %@", [error localizedDescription]);
    }
}


@end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top