iPhone - NSUndoManager + NS 调用 +对象释放=崩溃
我正在为我的应用程序构建撤消/重做功能。我正在使用 NSUndoManager 的 NSInitation 方法。
这就是我构建调用的方式,
NSNumber *firstState = [NSNumber numberWithInt:fsNumber];
NSInvocation *initialState = [self restoreStateInvocation:firstState];
// ... the code continues...
这些是当UNDO/REDO调用initialState时相关的方法,
- (NSInvocation *) restoreStateInvocation:(NSNumber*)number {
NSMethodSignature *executeMethodSignature = [self methodSignatureForSelector:
@selector(makeUNDO:)];
NSInvocation *moveInvocation = [NSInvocation invocationWithMethodSignature: executeMethodSignature];
[moveInvocation setTarget:self];
[moveInvocation setSelector:@selector(makeUNDO:)];
[moveInvocation setArgument:&number atIndex:2];
return moveInvocation;
}
- (void) makeUNDO:(NSNumber*)number {
int num = (int)[number intValue];
// code crashes at this line... number appears to be deallocated at this time
//
...
}
应用程序在makeUNDO的第一行崩溃,如代码所示。
如何保留号码而不泄露?
谢谢。
I am building a undo/redo functionality for my app. I am using the NSInvocation method of NSUndoManager.
This is how I build the invocation
NSNumber *firstState = [NSNumber numberWithInt:fsNumber];
NSInvocation *initialState = [self restoreStateInvocation:firstState];
// ... the code continues...
these are the methods relates
- (NSInvocation *) restoreStateInvocation:(NSNumber*)number {
NSMethodSignature *executeMethodSignature = [self methodSignatureForSelector:
@selector(makeUNDO:)];
NSInvocation *moveInvocation = [NSInvocation invocationWithMethodSignature: executeMethodSignature];
[moveInvocation setTarget:self];
[moveInvocation setSelector:@selector(makeUNDO:)];
[moveInvocation setArgument:&number atIndex:2];
return moveInvocation;
}
- (void) makeUNDO:(NSNumber*)number {
int num = (int)[number intValue];
// code crashes at this line... number appears to be deallocated at this time
//
...
}
when the UNDO/REDO calls initialState the app crashes on the first line of makeUNDO, as pointed on the code.
how can I retain number without leaking?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的答案是将以下行添加到restoreStateInvocation...
the correct answer is to add the following line to restoreStateInvocation...
您可以保留
NSNumber
对象,因为它是从NSObject
继承的。使用完 myNumber 后,您还需要
release
。[myNumber 释放]
。编辑:
使用下面描述的方法...
您可以将myNumber作为您的班级成员。
在 .h 中
在实现文件(.m 文件)中。
you could retain
NSNumber
object because it's inherited fromNSObject
.you also need to
release
once you finished using myNumber.[myNumber release]
.EDITED:
Use the approach described below...
you could have myNumber as your class member.
in .h
In implementation file (.m file).