你问:
是否有其他替代方法可以从其他类别获取价值?
简而言之,使用“getter”是从另一个类获取值的惯用方法。但是看看你的问题(承认,没有足够的源代码来正确诊断你的问题),我猜测问题在于实例变量的使用。但稍后会详细介绍。
首先我们来看看正确的使用方法声明的属性 http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/ocproperties.html及其实例变量和访问器方法(getter 和 setter)。通常,您应该使用这些访问器方法来设置属性。 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW5不过,您可以使用实例变量(并且您不应在初始化程序和 dealloc 方法中使用访问器方法 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW6)。并且在使用getter和setter时,可以选择是否使用方法调用(例如“[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
然后我们来演示如何使用value
的财产CustomClass
object customObject
从你的视图控制器中:
@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是另一个类,variable是一个属性,并且是int类型。我得到了这个类中变量的值,但是当我将它更改为 self.customclass.variable 时,我总是得到 0。
好吧,这可能是由几个不同的问题引起的,但我看到的最常见的问题是显式声明的实例变量和由幕后创建的实例变量之间的混淆@synthesize
陈述。这就是为什么我总是建议人们不要为其声明的属性显式定义实例变量,而是让@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
,当编译器命中@synthesize
声明,它创建了another实例变量,这次没有前导下划线,customObject
。因此,我显式声明的实例变量从未收到 init/alloc,因此是nil
因此任何使用它的尝试都不会成功!
通常我们会看到相反的问题(显式声明的实例变量没有下划线和@synthesize
表格的陈述@synthesize customObject = _customObject
),但希望你能明白这个想法。
无论如何,这是导致您所描述的行为的最常见示例。如果情况并非如此,请向我们提供更广泛的代码示例。
但如果您遇到问题,我总是建议您检查一下您的价值CustomClass
对象本身,然后再尝试访问其属性。在尝试使用类对象的属性之前,请确保类对象本身已正确初始化(无论是出于上面列出的原因还是其他一些初始化问题)。你可以做类似的事情NSLog(@"CustomClass object = %@", customObject);
or NSAssert(customObject, @"Object not properly initialized");
.