iOS 对象归档自始至终 [英] iOS Object Archiving from start to finish

查看:18
本文介绍了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. 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天全站免登陆