我有这个功能:
-(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;
}
当我在return 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;
保留不够?
答案 0 :(得分:0)
方法decodeObjectForKey:
返回自动释放的对象,因此您需要保留它。下面找一个例子。对所有实例变量执行此操作:
_link = [[decoder decodeObjectForKey:@"_link"] retain];
答案 1 :(得分:0)
尝试使用此代码:
self.link = [decoder decodeObjectForKey:@"_link"];
self.trigger= [decoder decodeObjectForKey:@"_trigger"];
//etc
我遇到了同样的问题。这段代码对我有帮助。 decodeObjectForKey:
方法返回的对象是自动释放的。将其分配给属性将保留它。