Error content:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Dmain.LDevice encodeWithCoder:]: unrecognized selector sent to instance 0x7f811c717ca0'
*** First throw call stack:
实际数据采用此格式,并保存为NSMutableArray
。
<Dmain.LDevice: 0x7fa9f5761fc0>,
我尝试将其保存为NSUserDefault
,我正在寻找另一种方式,因为发生了App Crash。
在搜索过程中,我们使用NSCoding
进行编码,并找到了一种使用解码的方法。
但是,与上述错误相同的错误将继续发生。
以下是我写的来源。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *stringRootPath = [documentsDirectory stringByAppendingFormat:@"/metaDeviceList"];
ArchiveData *archiveData = [ArchiveData new];
archiveData.metaDeviceList = [_metaDeviceList mutableCopy];
NSMutableData *mutableData = [NSMutableData new];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
[archiver encodeObject:archiveData forKey:@"metaDeviceList"]; // App crush
[archiver finishEncoding];
// [NSKeyedArchiver archiveRootObject:archiveData toFile:stringRootPath]; // App crush
[mutableData writeToFile:stringRootPath atomically:YES];
ArchiveData的源内容。
(ArchiveData.h)
#import <Foundation/Foundation.h>
@interface ArchiveData : NSObject <NSObject,NSCoding>
{
NSObject *_metaDeviceList;
}
@property (nonatomic, retain) NSObject *metaDeviceList;
@end
(ArchiveData.m)
#import "ArchiveData.h"
@implementation ArchiveData
- (void)dealloc{
[self setMetaDeviceList:nil];
}
- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
[aCoder encodeObject:self.metaDeviceList forKey:@"metaDeviceList"];
}
- (id)initWithCoder:(nonnull NSCoder *)aDecoder {
self = [super init];
if (self != nil) {
self.metaDeviceList = [aDecoder decodeObjectForKey:@"metaDeviceList"];
}
return self;
}
@end
有什么问题?请让我知道如何解决它!请...