标签: nskeyedarchiver

如何使用NSPropertyListXMLFormat_v1_0格式的NSKeyedArchiver将对象序列化为文件?

希望这个标题足够清楚.无论如何,似乎:

[NSKeyedArchiver archiveRootObject:rootObject toFile:path];
Run Code Online (Sandbox Code Playgroud)

仅限于NSPropertyListBinaryFormat_v1_0.

我需要格式是人类可读的[所以xml].如果您有兴趣,这是项目的一部分,我最终会在github上刊登一篇关于"如何对GPS应用进行单元测试"的博客文章

:-)

cocoa objective-c nskeyedarchiver

0
推荐指数
2
解决办法
5222
查看次数

游戏状态singleton cocos2d,initWithEncoder总是返回null

我正在尝试在cocos2d中编写一个基本测试"游戏状态"单例,但由于某些原因加载应用程序时,从不调用initWithCoder.任何帮助将不胜感激,谢谢.

这是我的单身GameState.h:


#import "cocos2d.h"

@interface GameState : NSObject <NSCoding>
{
  NSInteger level, score;
  Boolean seenInstructions;
}

@property (readwrite) NSInteger level;
@property (readwrite) NSInteger score;
@property (readwrite) Boolean seenInstructions;

+(GameState *) sharedState;
+(void) loadState;
+(void) saveState;

@end
Run Code Online (Sandbox Code Playgroud)

...和GameState.m:


#import "GameState.h"
#import "Constants.h"

@implementation GameState

static GameState *sharedState = nil;

@synthesize level, score, seenInstructions;

-(void)dealloc {
  [super dealloc];
}

-(id)init {
  if(!(self = [super init]))
    return nil;  
  level = 1;
  score = 0;
  seenInstructions = NO;

  return self;
}

+(void)loadState {
  @synchronized([GameState …
Run Code Online (Sandbox Code Playgroud)

singleton nskeyedarchiver cocos2d-iphone nsdata iphone-sdk-3.0

0
推荐指数
1
解决办法
3539
查看次数

如何用NSKeyedArchiver保存我自己的对象?

我使用NSKeyedArchiver/NSKeyedUnarchiver来保存我的对象时遇到了问题.
我添加了方法- (id)initWithCoder:(NSCoder *)decoder- (void)encodeWithCoder:(NSCoder *)encoder.问题是当我尝试保存对象时它无法正常工作.
我可以想象一个问题(但我不确定它是否是问题;)).我的对象中有一些数组.它们包含更多的对象(我也实现了两个"Coder"-Methods).那么数组是否调用了它的对象中的方法?

任何可能的解决方

谢谢!!

arrays iphone objective-c save nskeyedarchiver

0
推荐指数
1
解决办法
941
查看次数

decodeObjectForKey:始终返回nil

我试图将我的对象图保存到一个文件,然后在以后重新加载它,但是decodeObjectForKey:对于我指定的任何键,总是返回nil.

创建了一个二进制文件,其中偶尔会有人类可读的文本,即titleTextColor,所以我认为归档过程正在运行.

我是否想念NSKeyedArchiver和NSKeyedUnarchiver是如何工作的?任何帮助,将不胜感激.

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeFloat:titleFontSize forKey:@"titleFontSize"];
    [encoder encodeObject:[UIColor orangeColor] forKey:@"titleTextColor"];
    [encoder encodeObject:lineSeparatorColor forKey:@"lineSeparatorColor"];
    [encoder encodeObject:bodyTextColor forKey:@"bodyTextColor"];
    [encoder encodeFloat:bodyTextFontSize forKey:@"bodyTextFontSize"];
    [encoder encodeObject:backgroundColor forKey:@"backgroundColor"];
    [encoder encodeObject:tintColor forKey:@"tintColor"];
    [encoder encodeInteger:bodyTextAlignment forKey:@"bodyTextAlignment"];

    [encoder encodeObject:@"Text" forKey:@"Text"];
}

+ (void) saveToFile {
    // Get the shared instance
    PSYDefaults *sharedInstance = [PSYDefaults sharedInstance];    

    // Serialise the object
    NSData *serializedObject = [NSKeyedArchiver archivedDataWithRootObject:sharedInstance];

    // Get the path and filename of the file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString …
Run Code Online (Sandbox Code Playgroud)

iphone nscoding nskeyedarchiver ios

0
推荐指数
1
解决办法
5477
查看次数

编码NSAttributedString会引发错误

基于对此问题的接受答案,我编写了以下代码:

NSData* somedata;
somedata=[NSKeyedArchiver archivedDataWithRootObject:ts];
Run Code Online (Sandbox Code Playgroud)

其中ts是一个NSAttributedString,它填充了一些文本和一些属性(在本例中为颜色).

当我执行此代码时,我收到此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x6eb5b90'
Run Code Online (Sandbox Code Playgroud)

我是NSCoder竞技场的新手,但上述问题的答案使我觉得这就是我所要做的.是吗?我错过了什么?


编辑:

在这种情况下,无法识别的选择器被发送到NSAttributedString中的颜色属性.当我像这样初始化字符串时:

NSAttributedString *ts = [[NSAttributedString alloc] initWithString:text attributes:self.currentAttributeDictionary];
Run Code Online (Sandbox Code Playgroud)

字典就像这样构建:

self.currentAttributeDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
                                 [self.currentColor CGColor],(NSString*)kCTForegroundColorAttributeName,
                                 nil];
Run Code Online (Sandbox Code Playgroud)

字典的NSLog产生了这个:

New dictionary is: {
CTForegroundColor = "<CGColor 0x6eb5b90> [<CGColorSpace 0x6e968c0> (kCGColorSpaceDeviceRGB)] ( 1 1 0 1 )";}
Run Code Online (Sandbox Code Playgroud)

上面的CGColor的地址与错误消息中的地址匹配.

objective-c nsattributedstring nskeyedarchiver nscoder

0
推荐指数
1
解决办法
3014
查看次数