I have some @properties that are behaving strangely in the debugger when I @synthesize
them and then subclass the controller.
The @properties behave as expected when I remove @synthesize.
Here is a basic example, a ViewController
subclass called SubClassedViewController
.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
NSNumber *someNumber;
}
@property (nonatomic, strong) NSNumber *someNumber;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize someNumber; //Strange behavior unless I remove this line
- (void)viewDidLoad
{
[super viewDidLoad];
someNumber = [NSNumber numberWithInt:123];
// Do any additional setup after loading the view, typically from a nib.
}
@end
SubClassedViewController.h
#import "ViewController.h"
@interface SubClassedViewController : ViewController
@end
SubClassedViewController.m
#import "SubClassedViewController.h"
@interface SubClassedViewController ()
@end
@implementation SubClassedViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
Behavior with @synthesize
Unless I use self.
, the object points to the subclassed controller, not to the NSNumber:
(lldb) po someNumber
$3 = 0x07151a30 <SubClassedViewController: 0x7151a30>
(lldb) po self.someNumber
$4 = 0x08c37f20 123
(lldb) p (int)[someNumber intValue]
error: Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector.
The process has been returned to the state before execution.
(lldb) po (UIView*)[someNumber view]
$7 = 0x071574f0 <UIView: 0x71574f0; frame = (0 20; 320 548); autoresize = RM+BM; layer = <CALayer: 0x71575a0>>
Note that if I put NSLog
s in my code, someNumber correctly points to the NSNumber; it is only identified as a SubClassedViewController in lldb (as far as I can tell).
Behavior without @synthesize
If I comment out @synthesize
and change the NSNumber creation in ViewController.m to:
_someNumber = [NSNumber numberWithInt:123]; //I added _ before variable name
Then I get the following different behavior (which is the behavior I'd expect)
(lldb) po _someNumber
$0 = 0x0717db70 123
(lldb) po self.someNumber
$1 = 0x0717db70 123
(lldb) po self
$2 = 0x071853a0 <SubClassedViewController: 0x71853a0>
Questions
- Is this a bug, or do I misunderstand something?
If this is a bug (which is my assumption)...
- Is it a bug in Xcode, the debugger, or the compiler?
If this is not a bug...
I'm using Xcode 4.6.1, which doesn't require that I @synthesize things. But shouldn't the behavior of my app be identical whether or not I do?
Why would it ever make sense for a property to point to its "owner"?