Objective-C setter 永远不会被调用
我正在尝试使 NSMutableArray 可在多个类中使用。我在定义和使用自定义设置器时遇到问题,由于某种原因,即使我调用我的设置器,它也永远不会执行(我在方法中设置了 NSLog)。以下是所有相关代码:
AppDelegate.h
@interface TouchTrackerAppDelegate : NSObject <UIApplicationDelegate> {
NSMutableArray *completeLines;
}
@property (nonatomic, retain, setter = setCompleteLines:, getter = getCompleteLines) NSMutableArray *completeLines;
-(NSMutableArray*) getCompleteLines;
-(void) setCompleteLines:(NSMutableArray *) newLines;
AppDelegate.m
@implementation TouchTrackerAppDelegate
-(NSMutableArray*) getCompleteLines {
return self.completeLines;
}
-(void) setCompleteLines:(NSMutableArray *)newLines {
NSLog(@"gets here");
if (completeLines != newLines) {
[completeLines release];
completeLines = [newLines retain];
}
NSLog(@"completeLines global count: %i",[completeLines count]);
}
View.h
#import "TouchTrackerAppDelegate.h"
@interface TouchDrawView : UIView {
NSMutableDictionary *linesInProcess;
NSMutableArray *completeLines;
TouchTrackerAppDelegate *navigationDelegate;
}
@end
*
#import "TouchTrackerAppDelegate.h"
- (id)initWithCoder:(NSCoder *)c
{
[super initWithCoder:c];
linesInProcess = [[NSMutableDictionary alloc] init];
completeLines = [[NSMutableArray alloc] init];
return self;
}
- (void)viewDidLoad {
navigationDelegate = (TouchTrackerAppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(void)endTouches:(NSSet *)touches
{
if([EditModeSingleton isEditMode]){
for(UITouch *t in touches){
NSValue *key = [NSValue valueWithPointer:t];
Line *line = [linesInProcess objectForKey:key];
if(line){
[completeLines addObject:line];
[linesInProcess removeObjectForKey:key];
[navigationDelegate setCompleteLines:completeLines];
NSLog(@"completeLines count: %i", [completeLines count]);
}
}
[self setNeedsDisplay];
}
else {NSLog(@"in Play mode");}
}
View.m 当我调用“[navigationDelegate setCompleteLines:completeLines];”时,我的 View.m 中出现问题。据我所知,这永远不会执行。我也不确定我的 setter 方法是否正确,因为我尝试将数组从我的视图传递到应用程序委托以在其他类中使用。如果有更好的方法,我将不胜感激。
谢谢你!
I'm trying to make an NSMutableArray usable in multiple classes. I'm having an issue with defining and using a custom setter, for some reason, even though I call my setter, it is never executed (I have an NSLog set up in the method). Here is all of the relevant code:
AppDelegate.h
@interface TouchTrackerAppDelegate : NSObject <UIApplicationDelegate> {
NSMutableArray *completeLines;
}
@property (nonatomic, retain, setter = setCompleteLines:, getter = getCompleteLines) NSMutableArray *completeLines;
-(NSMutableArray*) getCompleteLines;
-(void) setCompleteLines:(NSMutableArray *) newLines;
AppDelegate.m
@implementation TouchTrackerAppDelegate
-(NSMutableArray*) getCompleteLines {
return self.completeLines;
}
-(void) setCompleteLines:(NSMutableArray *)newLines {
NSLog(@"gets here");
if (completeLines != newLines) {
[completeLines release];
completeLines = [newLines retain];
}
NSLog(@"completeLines global count: %i",[completeLines count]);
}
View.h
#import "TouchTrackerAppDelegate.h"
@interface TouchDrawView : UIView {
NSMutableDictionary *linesInProcess;
NSMutableArray *completeLines;
TouchTrackerAppDelegate *navigationDelegate;
}
@end
View.m*
#import "TouchTrackerAppDelegate.h"
- (id)initWithCoder:(NSCoder *)c
{
[super initWithCoder:c];
linesInProcess = [[NSMutableDictionary alloc] init];
completeLines = [[NSMutableArray alloc] init];
return self;
}
- (void)viewDidLoad {
navigationDelegate = (TouchTrackerAppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(void)endTouches:(NSSet *)touches
{
if([EditModeSingleton isEditMode]){
for(UITouch *t in touches){
NSValue *key = [NSValue valueWithPointer:t];
Line *line = [linesInProcess objectForKey:key];
if(line){
[completeLines addObject:line];
[linesInProcess removeObjectForKey:key];
[navigationDelegate setCompleteLines:completeLines];
NSLog(@"completeLines count: %i", [completeLines count]);
}
}
[self setNeedsDisplay];
}
else {NSLog(@"in Play mode");}
}
The problem arises in my View.m when I call '[navigationDelegate setCompleteLines:completeLines];'. As far as I can tell, this never executes. I'm also not sure if my setter method is correct in the way I'm trying to pass the array from my view to the app delegate for use in other classes. If there is a better way of doing that, I'd appreciate some help.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有输入该函数,那么实际上只有一种可靠的可能性:
navigationDelegate
为零。通过在endTouches
中向其发送消息之前记录或断言它来验证这一点,然后找出原因。纳吉:
至:
If you're not entering that function, there's really only one solid possibility:
navigationDelegate
is nil. Verify this by logging or asserting it just before sending the message to it inendTouches
and then figure out why.Cnage:
To:
供将来参考/帮助(并在评论中回答您的问题) -
断点基础知识简介:
如果不出意外,断点可以通过排除过程缩小问题范围,并帮助您提出更好的问题,从而更快地得到解答。 =)
虽然 StackOverflow 帮助您很快地找到了这个问题,但是如果您使用断点,您可以在将来节省大量时间并减少挫败感。
在这种情况下,在以下行处或之前设置断点:
[navigationDelegate setCompleteLines:completeLines];
会显示navigationDelegate
为 nil。然后重复:在分配 navigationDelegate 时或之前设置断点并重新运行它。当这个断点没有被命中时,你就会意识到你的问题不是你的setter! =)您可能仍然需要问“为什么 viewDidLoad 没有被调用?”但由于您已经解决了部分困惑,您的答案会更快到达!希望对以后的你有帮助~
For future reference/help (and to answer your question in comments) -
Breakpoint basics in brief:
If nothing else, breakpoints can narrow your issue down by process of elimination and help you ask better questions that get answered faster. =)
Although StackOverflow helped you track down this problem pretty fast, you can save yourself a lot of time and frustration in the future if you make use of breakpoints.
In this case, setting a breakpoint at or before the line:
[navigationDelegate setCompleteLines:completeLines];
would have revealednavigationDelegate
was nil. Then you repeat: set a breakpoint at or beforenavigationDelegate
is assigned and re-run it. When this breakpoint didn't get hit, you would then realize your problem is something other than your setter! =)You might still have had to ask "why isn't viewDidLoad being called?" but with part of the confusion already solved by you, your answer would have arrived much faster! Hope that helps you in the future~