相关疑难解决方法(0)

为什么子类@property没有相应的ivar隐藏超类ivars?

以下似乎很简单.有一个带有ivar的超类,以及一个访问(@protected)超类ivar的子类:

// Testclass.h    
@interface TestClass : NSObject {
    NSString *testIvar;
}
@end

//TestClass.m
@implementation TestClass
@end

//TestSubclass.h
@interface TestSubClass : TestClass {
}

@property (nonatomic, retain) NSString *testProperty;
- (void) testMethod;

@end

//TestSubclass.m    
#import "TestSubClass.h"
@implementation TestSubClass

@synthesize testProperty;

- (void) testMethod{
    NSLog(@"The value was: %@", testIvar);
}
@end
Run Code Online (Sandbox Code Playgroud)

简单而正确 - 看似足够.但是,尝试编译(对于iOS 4.2 SDK,使用GCC 4.2)会产生指向NSLog行的错误:'testIvar unclared'.

我是Objective-C的新手,但不能为我的生活看到为什么这应该是一个错误.注释掉testProperty的东西,它编译好了.似乎在没有相应的ivar的子类中添加合成属性实际上隐藏了一个不相关的超类ivar.

任何人都可以告诉我这里发生了什么?相关的,编译错误是否可以预见?(预见它可以节省我一些时间和挫折).

objective-c ios4 ios

12
推荐指数
1
解决办法
1900
查看次数

@pronties在@synthesized时在调试器中出现意外行为,然后在子类中访问

我有一些@properties在调试器中表现得很奇怪@synthesize,然后继承控制器.

当我删除@synthesize时,@ property会按预期运行.

这是一个基本的例子,一个ViewController叫做的子类SubClassedViewController.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    NSNumber *someNumber;
}

@property (nonatomic, strong) NSNumber *someNumber;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

SubClassedViewController.h

#import "ViewController.h"

@interface SubClassedViewController : ViewController

@end
Run Code Online (Sandbox Code Playgroud)

SubClassedViewController.m

#import "SubClassedViewController.h" …
Run Code Online (Sandbox Code Playgroud)

objective-c ios lldb

1
推荐指数
1
解决办法
2952
查看次数

标签 统计

ios ×2

objective-c ×2

ios4 ×1

lldb ×1