Why aren't my watchpoint working?

My device: iOS11.3.1,iPhone7P

Xcode: version 9.4.1, configuration is default.


demo:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, assign) int count;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self test];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)test
{
    NSLog(@"hi.");
    
    self.count = 5; // watch it!
    NSLog(@"count: %d", self.count);
    
    NSLog(@"bye.");
}


I want to watch variable "count",but failed.
NOTE: breakpoint is OK.


And there is a situation to explain: In the same environment,iOS10 watchpoint can work
How to solve it? thx

You’re mixing up properties and instance variables (ivars). A watchpoint is set on memory, which means you need to target a variable (like an ivar). A property isn’t a variable, it’s a pair of methods, a getter that reads the property and a setter that sets it. In many cases, including this one, these methods are synthesised by the compiler to read and write an ivar, but the watchpoint mechanism doesn’t know about that.

So, if you want to set a watchpoint on

count
you have a couple of options:
  • You could write an explicit getter and setter for the

    count
    property, and then set a breakpoint on the setter. For example, if you write a getter and setter like this:
    - (int)count {
        return self->_count;
    }
    
    
    - (void)setCount:(int)newValue {
        self->_count = newValue;
    }

    you can set a breakpoint on line 7.

    Note To get this to compile you’ll need to explicitly declare your ivar:

    @implementation ViewController {
        int _count;
    }

    .

  • You could set a watchpoint on the

    _count
    ivar. This is a bit tricky because of the way that synthesised ivars are seen by the debugger. I got it working by adding code that prints the address of the ivar:
    NSLog(@"%p", &self->_count);

    and then setting a watchpoint on that address:

    watchpoint set expression 0x7f993b408570

    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Why aren't my watchpoint working?
 
 
Q