- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 NSCoding 来保存和恢复应用程序状态。我以前没用过。
在我的应用程序中,协议(protocol)方法 encodeWithCoder 和 initWithCoder 从未被调用。我已经准备了一个具有相同问题的简单测试用例,希望有人能告诉我我做错了什么。
这是我的 CodingTest.h 文件:
#import <Foundation/Foundation.h>
@interface CodingTest : NSObject <NSCoding>
- (void) saveData;
- (void) loadData;
- (id) init: (int) testValue;
@end
这里是 CodingTest.m
#import "CodingTest.h"
@interface CodingTest()
@property int testInt;
@end
@implementation CodingTest
- (id) init: (int) testValue
{
_testInt = testValue;
return self;
}
-(void) loadData
{
CodingTest *newTestClass = [NSKeyedUnarchiver unarchiveObjectWithFile:@"testfile"];
}
-(void) saveData
{
[NSKeyedArchiver archiveRootObject:self toFile:@"testfile"];
}
- (void) encodeWithCoder:(NSCoder *)encoder {
[encoder encodeInt:_testInt forKey:@"intValue"];
}
- (id)initWithCoder:(NSCoder *)decoder {
int oldInt = [decoder decodeIntForKey:@"intValue"];
return [self init:oldInt];
}
@end
我这样调用它:
CodingTest *testCase = [[CodingTest alloc] init:27];
[testCase saveData ];
[testCase loadData];
init、saveData 和 loadData 都被调用了。但是永远不会调用 encodeWithEncoder 和 initWithCoder。我做错了什么?
最佳答案
问题是“testfile”本身不是一个有效的文件名。如果将其更改为“tmp/testfile”,它就可以正常工作。
有趣的是,如果在编码时得到错误的文件名,它不会调用解码函数,即使解码调用没有指定文件名。
关于ios nsCoding 设置但不调用 encodeWithCoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28524108/
您好,我在我的 ios 应用程序中使用 GoogleMaps,并且有一部分我必须在 CoreData 中保存 map 标记位置.我正在使用代码 GMSMarker *marker = [GMSMark
假设我有一个对象 Person。(我知道 Person 有语法错误。请忽略这些错误。) class Person: NSObject, NSCoding { var name : String
我有一个对象,其属性可能为 nil。我应该如何在 encodeWithCoder(和 decodeWithCoder)中实现它? - (void)encodeWithCoder:(NSCoder *)
我正在尝试使用 NSCoding 来保存和恢复应用程序状态。我以前没用过。 在我的应用程序中,协议(protocol)方法 encodeWithCoder 和 initWithCoder 从未被调用。
我第一次使用编码是为了将数据保存在我的应用程序之外。我一直在看的例子说要为自定义类调用 [super encodeWithCoder:coder]; 。 我有一个扩展 NSObject 的类,但收到下
我遇到了这个错误,但我不知道如何修复它。我刚刚添加了来自 Apple 示例 (LazyTableImages) 的延迟加载以及来自照片位置示例项目的 UIImageToDataTransformer
这将保存一个数组(我认为)。 - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:myArray forKey
我正在尝试用restkit中的值填充字典。我在restEngine.m中使用NSUserDefaults保存字典。 NSDictionary *dict = [mappingResult dicti
我在重写 encodeWithCoder: 方法时遇到了一个非常奇怪的崩溃。你可以在 https://github.com/reidmain/FDModel/blob/193b0661f7ab1ce6
我们正在开发一款可以将大量内容保存到 Core Data 中的应用程序。它保存的东西主要是NSDate NSNumber & NSString 但它也将很多图片和视频保存为NSData。我正在尝试更改
我知道当你编写实现 NSCoding 的对象的子类的 initWithCoder 方法时,你必须调用 super initWithCoder(而不是 super init),但是我必须在encodeW
尝试使用 NSCoder 时出现错误 Player.swift: class Player: NSObject, NSCoding { private var _playerName: Str
我的自定义对象使用以下方法符合 NSCoding 协议(protocol) required init(coder decoder: NSCoder) { super.init() c
这个问题已经有答案了: NSKeyedUnarchiver unarchiveObjectWithData:data returns non nil object but with nil prope
我正在尝试保存一个名为 queueArray 的 NSMutableArray,以便在应用程序退出后可以再次加载它。我使用了一些教程来帮助我前进,这是我想出的代码。问题似乎是“initWithCode
我有一个自定义类 MyClass,它基本上由几个 NSMutableArrays 组成,没有其他变量。我有一个实体 MyEntity,它有一个属于 MyClass 的 ivar。当我尝试保存实体时,我
错误: Assertion failure in -[UICGColor encodeWithCoder:], /SourceCache/UIKit/UIKit-2372/UIColor.m:1191
我有这个错误。我知道有些人已经在其他线程上回答了这个问题,但给出的答案似乎对我不起作用。 这是我的代码: 人物类: class People: NSObject, NSCoding { var
我知道有几个与此类似的问题,它们往往都围绕着未正确遵守协议(protocol)的类展开,但这不应该是这里的直接问题。 以下是目前给我这个问题的代码的浓缩版: enum Binary: Int {
我在实现 NSCoding 时遇到了问题。这是我的用户类代码: public class User: NSCoder { ... NSCoding 方法: 解码器: required public i
我是一名优秀的程序员,十分优秀!