gpt4 book ai didi

ios - 一些 NSCoding 变量未被检索

转载 作者:行者123 更新时间:2023-11-29 04:21:41 25 4
gpt4 key购买 nike

我正在使用 NSData 来存储对象以供以后使用。它有相当多的 NSString,我需要将其从对象中取出。
由于某种原因,只有 一些 的 NSString 被存储,而其他一些则被清零!我想这一定是我的代码的问题,我一定忘记初始化一些字符串,但由于一个非常奇怪的原因,一些字符串丢失了数据!我无法通过 theImportantString 获取它的相关值,因为它首先看起来像是变量获得了它的值,但是从 Unarchive 返回后,它等于 @""!

//CMyData.h
//////////////////////

@interface CMyData : NSObject <NSCoding>
{
NSString *ID;
NSString *DIST;
.
.
}
@property (nonatomic,retain) NSString *ID;
@property (nonatomic,retain) NSString *DIST;

@end

//CMyData.m
//////////////////////

#import "CMyData.h"
@implementation CMyData

@synthesize ID;
@synthesize DIST;

- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.ID = [decoder decodeObjectForKey:@"ID"];
self.DIST = [decoder decodeObjectForKey:@"DIST"];
.
.
}


- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:ID forKey:@"ID"];
[encoder encodeObject:DIST forKey:@"DIST"];
.
.

}

- (void)dealloc {
[ID release];
[DIST release];
[super dealloc];

}

@end

MyController.m

-(void) makeObject: (NSDictionary *)dict
{
CMyData* myData=[[CMyData alloc] init];
myData.ID = [[NSString alloc] initWithString:[dict objectForKey:@"NAME"]];
myData.DIST = [[NSString alloc] initWithString:[dict objectForKey:@"DISTRIBUTOR"]];
.
.

myObject = [[[MYObject alloc] init];

myObject.data = [NSKeyedArchiver archivedDataWithRootObject:myData];
}

然后点击按钮:

- (void) tapOnIcon: (MyObject*)theObject 
{
CMyData *data = [NSKeyedUnarchiver unarchiveObjectWithData:theObject.data];
[delegate showData:data];
}

在委托(delegate) Controller 中(无法再设置值):

delegateController.m
//////////////////////////////////

-(void) showData:(CMyData*)theData{
self.theImportantString = [[NSString alloc] initWithString:theData.DIST];
.
.
.


}

最佳答案

似乎您的类型不匹配:

// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [decoder decodeIntForKey:@"DIST"];

但是在声明中你有

// in CMyData.h
NSString *DIST;

这应该是:

// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [NSString stringWithFormat:@"%d", [decoder decodeIntForKey:@"DIST"]];

关于ios - 一些 NSCoding 变量未被检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12914198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com