iOS:其他替代实例variables?
我有一个其他人写的项目,我已经接pipe了它,希望能够让应用程序更好。
我遇到了一个问题:
来自一个class级:
我写_customclass.variable。 CustomClass是另一个类,variables是一个属性,是inttypes的。 我在这个类中获得了variables的值,但是当我将它改为self.customclass.variable时,我总是得到0.是否有其他替代方法从其他类中获取值?
(一个)
@property (readwrite)int boxSpacing;
(b)中
@synthesize boxSpacing;
(C)
- (id)initWithCoder:(NSCoder *)aDecoder { self.boxSpacing = 10; }
你问:
还有其他的替代方法来从其他class级获得价值吗?
简短的答案是使用“getter”是从另一个class级获得价值的习惯方式。 但看着你的问题(承认,没有足够的源代码来正确诊断你的问题),我猜这个问题在于使用实例variables。 但稍后更多。
首先,我们来看看正确使用声明的属性及其实例variables及其访问方法(getter和setter)。 通常你应该使用这些访问器方法来设置属性。 但是,您可以使用实例variables从类中访问variables(并且不应该在初始化方法和dealloc方法中使用访问器方法 )。 当使用getter和setter时,可以select是使用方法调用(例如“ [self customObject]
”)还是点表示法(例如“ self.customObject
”)。
我们来看一个例子。 假设你有一些简单的CustomClass
:
@interface CustomClass : NSObject { // you don't need to declare the instance variable //int _boxSpacing; } @property (nonatomic) int boxSpacing; @end @implementation CustomClass // In Xcode 4.4 and later, the synthesize statement is optional, and if you // omit it, it will synthesize the instance variable like this, with the // leading underscore. While you don't need to use an underscore in your // instance variable, it has become convention in iOS development and it's // a good technique to minimize chances that you accidentally use the instance // variable when you actual intended to use the property's accessor methods // (the getter and setter). @synthesize boxSpacing = _boxSpacing; @end
现在,假设您将从内部使用此CustomClass
,例如您的视图控制器。 所以,首先声明这个CustomClass
实例:
@interface MyViewController : UIViewController { // you do not need this instance variable declaration // the @synthesize statement will take care of this for you // CustomClass *_customObject; } @property (nonatomic, strong) CustomClass *customObject; @end
然后让我们演示如何在视图控制器中使用CustomClass
对象customObject
的value
属性:
@implementation MyViewController // Again, in Xcode 4.4 and later, the synthesize statement is optional, and if you // omit it, it will synthesize the instance variable like this, with the // leading underscore @synthesize customObject = _customObject; - (void)customClassTest { // initialize the object self.customObject = [[CustomClass alloc] init]; // set the property self.customObject.boxSpacing = 1; // finally, let's demonstrate three ways to retrieve the value NSLog(@"%d", self.customObject.boxSpacing); NSLog(@"%d", [[self customObject] boxSpacing]); NSLog(@"%d", _customObject.boxSpacing); // while we're at it, let's demonstrate other ways to set the property _customObject.boxSpacing = 2; // or [[self customObject] setBoxSpacing:3]; }
好吧,让我们回到你的问题。 你说:
我写_customclass.variable。 CustomClass是另一个类,variables是一个属性,是inttypes的。 我在这个类中获得了variables的值,但是当我将它改为self.customclass.variable时,我总是得到0。
好吧,这可能是由几个不同的问题引起的,但是我看到的最常见的问题是显式声明的实例variables和由@synthesize
语句在后台创build的实例variables之间的混淆。 这就是为什么我总是build议人们不要为他们声明的属性显式地定义实例variables,而是让@synthesize
语句自动执行。 这样我就不会有这样的问题,我将要展示。
考虑这个无害的(虽然不正确)的例子:
@interface MyViewController : UIViewController { CustomClass *_customObject; } @property (nonatomic, strong) CustomClass *customObject; @end @implementation MyViewController @synthesize customObject; - (void)customClassTestError { // initialize the object self.customObject = [[CustomClass alloc] init]; // this works self.customObject.boxSpacing = 1; // this doesn't! _customObject.boxSpacing = 2; // when it hits this statement, the value will still be 1!!! NSLog(@"%d", self.customObject.boxSpacing); }
你看到这个问题吗? 当我用下划线_customObject
声明一个实例variables时,当编译器碰到@synthesize
语句时,它创build了另一个实例variables,这次没有前导下划线customObject
。 因此,我明确声明的实例variables从来没有收到init / alloc,因此是nil
,因此任何尝试使用它都不会工作!
通常我们会看到相反的问题(一个显式声明的实例variables,没有下划线和@synthesize customObject = _customObject
forms的@synthesize
语句),但是希望你能明白。
无论如何,这是什么会导致你描述的行为最常见的例子。 如果不是这样,请提供更广泛的代码示例。
但是,如果遇到问题,我总是build议您在尝试访问它的属性之前检查自己的CustomClass
对象的值。 确保在尝试使用它的属性之前,类对象本身已经被正确初始化(无论是上面列出的原因还是其他一些初始化问题)。 你可以做一些像NSLog(@"CustomClass object = %@", customObject);
或NSAssert(customObject, @"Object not properly initialized");
。
你有没有为CustomClass定义@property? 如果是这样,你分配了一个值?
如果没有,那么你发送消息variable
nil
。 在你的情况下,将导致为0。