Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用编码器初始化后EXC\u错误\u访问_Ios_Objective C - Fatal编程技术网

Ios 使用编码器初始化后EXC\u错误\u访问

Ios 使用编码器初始化后EXC\u错误\u访问,ios,objective-c,Ios,Objective C,我有以下功能: -(id)initWithCoder:(NSCoder *)decoder { self = [super init]; if (self) { _smID = [[decoder decodeObjectForKey:@"_smID"]intValue]; _link = [decoder decodeObjectForKey:@"_link"]; _trigger = [decoder decod

我有以下功能:

-(id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        _smID = [[decoder decodeObjectForKey:@"_smID"]intValue];
        _link = [decoder decodeObjectForKey:@"_link"];        
        _trigger = [decoder decodeObjectForKey:@"_trigger"];
        _status = [decoder decodeObjectForKey:@"_status"];
        _expiration = [[decoder decodeObjectForKey:@"_expiration"]intValue];
        _timeFromRegistratio = [[decoder decodeObjectForKey:@"_timeFromRegistratio"]intValue];
        _timeFromSubsription = [[decoder decodeObjectForKey:@"_timeFromSubsription"]intValue];
        _timeFromLastOpening = [[decoder decodeObjectForKey:@"_timeFromLastOpening"]intValue];
        _timeToTrigger = [[decoder decodeObjectForKey:@"_timeToTrigger"]intValue];
    }
    return self;
}
当我在
返回self之前打印对象时。但是当它返回到调用线路时,对象已经消失了。这是电话线:

 SMBase *oldMonkeySurvayRule = [[NSKeyedUnarchiver unarchiveObjectWithData:oldMonkeySurvayRuleData]retain];
我得到了这个错误:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x60000008
0x0449709b in objc_msgSend ()
The program being debugged was signaled while in a function called from GDB.
GDB has restored the context to what it was before the call.
To change this behavior use "set unwindonsignal off"
Evaluation of the expression containing the function (_NSPrintForDebugger) will be abandoned.
以下是.h:

@interface SMBase : NSObject{
    NSString *_status;
    NSString *_trigger;
    int _expiration;
    int _timeFromRegistratio;
    int _timeFromSubsription;
    int _timeFromLastOpening;
    int _timeToTrigger;
    int _smID;
    NSString *_link;
}
@property  (nonatomic, strong) NSString *status;
@property  (nonatomic, strong) NSString *link;
@property  (nonatomic, strong) NSString *trigger;
@property  (nonatomic,assign)int expiration;
@property  (nonatomic,assign)int timeFromRegistratio;
@property  (nonatomic,assign)int timeFromSubsription;
@property  (nonatomic,assign)int timeFromLastOpening;
@property  (nonatomic,assign)int timeToTrigger;
@property  (nonatomic,assign)int smID;

还不够吗

方法
decodeObjectForKey:
返回自动删除的对象,所以您需要保留它。下面是一个例子。对所有实例变量执行此操作:

_link = [[decoder decodeObjectForKey:@"_link"] retain]; 
请尝试使用以下代码:

self.link = [decoder decodeObjectForKey:@"_link"]; 
self.trigger= [decoder decodeObjectForKey:@"_trigger"]; 
//etc

我也有同样的问题。这个代码对我有帮助。
decodeObjectForKey:
方法返回的对象是自动删除。将其分配给属性将保留它。

这与您的问题有关:)那么解决方案是什么?我给您提供了解决您问题的链接,,,顺便说一句,您的问题对我来说是新问题…因此我无法将其作为答案,但可能有助于您找到解决方案:)也许不是保留每个变量,我将以其他方式设置.h中的属性?你的意思是定义强属性,然后分配它们?如果是,它也会工作。这样可以吗?属性(非原子,强)NSString*状态;属性(非原子,强)NSString*链接;属性(非原子,强)NSString*触发器;属性(非原子,赋值)int到期;属性(非原子,赋值)int timefromRegistration;属性(非原子,赋值)int timefromscription;属性(非原子,赋值)int timefromLastOpen;属性(非原子,赋值)int timeToTrigger;属性(非原子,赋值)int-smID;没有帮助(我只更改了属性),即使您声明了属性,您仍在分配给IVAR。您需要保留或检查您的财产。e、 g.:self.link=[decoder decodeObjectForKey:@“_-link”];