Objective-C setter 永远不会被调用

发布于 2024-11-27 07:07:23 字数 2263 浏览 1 评论 0原文

我正在尝试使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-12-04 07:07:24

如果您没有输入该函数,那么实际上只有一种可靠的可能性:
navigationDelegate 为零。通过在 endTouches 中向其发送消息之前记录或断言它来验证这一点,然后找出原因。

纳吉:

    [linesInProcess removeObjectForKey:key];
    [navigationDelegate setCompleteLines:completeLines];

至:

    [linesInProcess removeObjectForKey:key];
    NSAssert(navigationDelegate != nil, @"navigationDelegate is nil");
    [navigationDelegate setCompleteLines:completeLines];

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 in endTouches and then figure out why.

Cnage:

    [linesInProcess removeObjectForKey:key];
    [navigationDelegate setCompleteLines:completeLines];

To:

    [linesInProcess removeObjectForKey:key];
    NSAssert(navigationDelegate != nil, @"navigationDelegate is nil");
    [navigationDelegate setCompleteLines:completeLines];
冰之心 2024-12-04 07:07:24

供将来参考/帮助(并在评论中回答您的问题) -

断点基础知识简介

  • 在您怀疑代码中断/失败/行为意外的行处或之前设置断点。在调试中运行程序...
  • 如果断点被击中:检查 Xcode 中各个调试窗格中的调用堆栈和变量值以获取线索。
  • 或者,如果从未命中断点:返回函数调用中的上一步并在那里设置断点。

如果不出意外,断点可以通过排除过程缩小问题范围,并帮助您提出更好的问题,从而更快地得到解答。 =)


虽然 StackOverflow 帮助您很快地找到了这个问题,但是如果您使用断点,您可以在将来节省大量时间并减少挫败感。

在这种情况下,在以下行处或之前设置断点: [navigationDelegate setCompleteLines:completeLines]; 会显示 navigationDelegate 为 nil。然后重复:在分配 navigationDelegate 时或之前设置断点并重新运行它。当这个断点没有被命中时,你就会意识到你的问题不是你的setter! =)

您可能仍然需要问“为什么 viewDidLoad 没有被调用?”但由于您已经解决了部分困惑,您的答案会更快到达!希望对以后的你有帮助~

For future reference/help (and to answer your question in comments) -

Breakpoint basics in brief:

  • Set breakpoints at or before the line where you suspect your code breaks/fails/behaves-unexpectedly. Run your program in debug...
  • If the breakpoint gets hit: Examine both the call-stack and variable-values in the various Debugging panes in Xcode for clues.
  • Or if the breakpoint is never hit: Go back up a step in your function calls and set a breakpoint there.

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 revealed navigationDelegate was nil. Then you repeat: set a breakpoint at or before navigationDelegate 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~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文