如何将NSManagedObjects的NSArray转换为NSData [英] How to convert an NSArray of NSManagedObjects to NSData

查看:90
本文介绍了如何将NSManagedObjects的NSArray转换为NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective C的新手,想知道是否有人可以帮助我。

I'm new to Objective C and was wondering if anyone can help me.

我使用sqlite数据库的核心数据来保存简单的配置文件对象一个名称和一个score属性(两者都是NSString类型)。

I am using core data with a sqlite database to hold simple profile objects which have a name and a score attribute (both of which are of type NSString).

我想做的是获取配置文件并将它们存储在NSData对象中,请参阅我的代码如下:

What I want to do is fetch the profiles and store them in an NSData object, please see my code below:

            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

            NSEntityDescription *entity = [NSEntityDescription entityForName:@"GamerProfile" inManagedObjectContext:managedObjectContext];
            [fetchRequest setEntity:entity];

            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
            NSArray *sortDecriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
            [fetchRequest setSortDescriptors:sortDecriptors];

            [sortDescriptor release];
            [sortDecriptors release];           

            NSError *error;
            NSArray *items = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:items];

            [self SendData:data];
            [fetchRequest release];

当我运行代码时,我得到错误终止应用程序由于未捕获异常'NSInvalidArgumentException' ,原因:'*** - [GamerProfile encodeWithCoder:]:无法识别的选择器发送到实例0x3f4b530'

When I run the code I'm getting the error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[GamerProfile encodeWithCoder:]: unrecognized selector sent to instance 0x3f4b530'"

我假设我必须添加一个encodeWithCoderClass到我的核心数据NSManagedObject对象(GamerProfile),但我不知道如何做这甚至在阅读文档,我这样做的尝试如下。我不知道如果我沿着正确的行与此作为警告,说明NSManagedObject可能不响应-encodeWithCoder

I presume I have to add an encodeWithCoderClass to my core data NSManagedObject object (GamerProfile) but I'm not sure how to do this even after reading the documentation, My attempt at doing this is below. I'm not sure if I'm going along the right lines with this as get a warning stating "NSManagedObject" may not respond to '-encodeWithCoder'"

我会

感谢

C

这里是我的GamerProfile(CoreData NSManagedObject对象)的代码,我试图添加一个encodeWithCoder方法...

Here is the code for my GamerProfile (CoreData NSManagedObject Object) with my attempt at adding an encodeWithCoder method...

Header File

Header File

#import <CoreData/CoreData.h>


@interface GamerProfile :  NSManagedObject <NSCoding>
{
}

@property (nonatomic, retain) NSString * GamerScore;
@property (nonatomic, retain) NSString * Name;

- (void)encodeWithCoder:(NSCoder *)coder;


@end

代码文件

#import "GamerProfile.h"


@implementation GamerProfile 

@dynamic GamerScore;
@dynamic Name;

- (void)encodeWithCoder:(NSCoder *)coder {

    [super encodeWithCoder:coder];

    [coder encodeObject:GamerScore forKey:@"GamerScore"];
    [coder encodeObject:Name forKey:@"Name"];



}


推荐答案

NSManagedObject和NSCoding真的不能一起玩。考虑此答案对类似问题的背景和一个可能的解决方案。

NSManagedObject and NSCoding really do not play well together. Consider this answer to a similar question for background and a possible solution.

这篇关于如何将NSManagedObjects的NSArray转换为NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆