从头到尾的iOS对象归档 [英] iOS Object Archiving from start to finish

查看:72
本文介绍了从头到尾的iOS对象归档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS应用,涉及保存和检索包含我创建的单个自定义对象的多个实例的NSMutableArray.我看过一些指南,例如 Apple的文档

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

我了解了如何执行操作(我认为),似乎我必须使用归档,因为数组中的对象不是原始变量,因此我已经使我的对象符合NSCoding.但是,我也看到了使用NSDefaults或不了解的示例(我没有文件IO经验).看到所有这些信息之后,我很难将所有内容拼接在一起.寻找的是从头到尾的示例程序的完整指南,该程序可以成功地使用归档来保存和检索自定义对象(无论是否以数组形式).如果有人可以为我指出一个很好的指南,或者自己写这篇文章,那将不胜感激!谢谢大家,Stack Overflow是一个很棒的地方!

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.如果需要更多信息,请在评论中告诉我!

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

推荐答案

请确保您要存档的任何类都实现了NSCoding协议,然后执行以下操作:

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

然后,我使用一个名为FileUtils的类来进行归档工作:

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

这篇关于从头到尾的iOS对象归档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆