开发者

Objective-C setter is never called

开发者 https://www.devze.com 2023-03-24 01:55 出处:网络
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 ha

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(@"completeLine开发者_运维问答s 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!


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];


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~

0

精彩评论

暂无评论...
验证码 换一张
取 消