iOS对象归档从头到尾[关闭]

Posted

技术标签:

【中文标题】iOS对象归档从头到尾[关闭]【英文标题】:iOS Object Archiving from start to finish [closed] 【发布时间】:2013-09-12 20:04:44 【问题描述】:

我正在开发一个 ios 应用程序,该应用程序涉及保存和检索 NSMutableArray,其中包含我创建的单个 custom 对象的多个实例。我看过几个指南比如Apple's Documentation

我明白了如何去做(我认为),看来我必须使用归档,因为我的数组中的对象不是原始变量,因此我已经使我的对象符合 NSCoding 标准。但是,我也看到了使用 NSDefaults 或任何我不理解的示例(我没有文件 IO 经验)。在看到所有这些信息后,我很难将所有内容拼凑在一起。我正在寻找的是一个完整指南,从头到尾,一个示例程序成功地使用归档来保存和检索自定义对象(在数组中或不在数组中)。如果有人可以为我指出一个很好的指南或在这篇文章中自己制作,那将不胜感激!谢谢大家,Stack Overflow 是一个很棒的地方!

附:如果需要更多信息,请在 cmets 中告诉我!

【问题讨论】:

考虑一下使用 Core Data 你得到免费的对象归档 【参考方案1】:

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

@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 的类来完成我的归档工作:

@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

【讨论】:

什么是 FileUtils 的子类呢?因为我假设它是一个标准的 Objective-c 类。 FileUtils 只是一个实用程序类...注意所有方法都是类方法。 完美,当我使用这些方法时,我应该为 NSString inFileName 传递什么?我假设 NSObject inObject 是我要正确归档的对象?非常感谢,这很有帮助! 是的,inFilename 是您要调用的文件,inObject 是您要归档的文件。 这是一个可靠的答案。为了清楚起见,我会说稍微重命名方法会有所帮助。 “inObject”有点让人觉得你要在那个对象内部做点什么。但是这里的 cmets 理所当然地澄清了这一点。干得好。

以上是关于iOS对象归档从头到尾[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

iOS 对象的归档

iOS学习 --- iOS12对象序列化(NSKeyedArchiver/NSKeyedUnarchiver)

iOS学习 --- iOS12对象序列化(NSKeyedArchiver/NSKeyedUnarchiver)

iOS 对模型对象进行归档

iOS -- 对象归档(将对象存入文件中)

iOS归档使用