简体   繁体   English

Objective-C不创建合成变量

[英]Objective-C Not Creating Synthesized Variables

I'm a beginning iOS developer, and still getting accustomed to this concept of synthesized variables and XCode automatically creating variables and setter/getter methods. 我是一个初学的iOS开发人员,并且仍然习惯于这个合成变量的概念和XCode自动创建变量和setter / getter方法。 I did quite a bit of research but was not able to find an answer that addressed what I'm facing. 我做了很多研究,但未能找到解决我面临的问题的答案。

I created a header class as follows: 我创建了一个头类,如下所示:

#import "Card.h"

@interface PlayingCard : Card

@property (strong, nonatomic) NSString *suit;
@property (nonatomic) NSUInteger rank;

@end

And I have the following implementation class: 我有以下实现类:

#import "PlayingCard.h"

@implementation PlayingCard

- (NSString *)contents
{
    NSArray *rankStrings = @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"J",@"Q",@"K"];
    return [rankStrings[self.rank] stringByAppendingString:self.suit];
}

- (void)setSuit:(NSString *)suit
{
    if([@[@"♥︎",@"♦︎",@"♠︎",@"♣︎"] containsObject:suit]) {
        _suit = suit;
    }
}

- (NSString *)suit
{
    return _suit ? _suit : @"?";
}

@end

My error is, whenever I use the _suit variable I get an error from XCode saying: 我的错误是,每当我使用_suit变量时,我从XCode得到一个错误:

Use of undeclared identifier '_suit'; did you mean 'suit'?

It was my understanding that _suit is generated automatically by the compiler and I should be able to access the "suit" property defined in the header file with "_suit". 我的理解是_suit是由编译器自动生成的,我应该能够使用“_suit”访问头文件中定义的“suit”属性。 Is it because I'm overriding the compiler's automatically generated setter and getter methods? 是因为我重写了编译器自动生成的setter和getter方法吗? Changing "_suit" to "self.suit" seems to fix the problem, but I'm confused as to why it seems that my underscore synthesized variable is not being generated. 将“_suit”更改为“self.suit”似乎可以解决问题,但我很困惑为什么看起来我的下划线合成变量没有被生成。 Any insight to this would be greatly appreciated, thanks! 对此有任何见解将不胜感激,谢谢!

If you manually create both accessors (the setter and the getter) for an @property , the compiler assumes you don't need/want it to synthesize them -- and the corresponding instance variable -- for you. 如果为@property手动创建两个访问器(setter和getter),编译器会假定您不需要/希望它为您合成它们 - 以及相应的实例变量。 There are two possible solutions. 有两种可能的解决方案。 Either declare the instance variable yourself: 要么自己声明实例变量:

@implemntation PlayingCard
{
    NSString *_suit;
}

Or, my preferred approach, use an explicit @synthesize statement above your custom accessors to tell the compiler that you do still want to synthesize an instance variable for the property: 或者,我的首选方法是在自定义访问器上方使用显式的@synthesize语句告诉编译器您仍然想要为该属性合成实例变量:

@synthesize suit = _suit;

Note that the = _suit is necessary because for legacy reasons, a simple @synthesize suit; 注意= _suit是必要的,因为由于遗留的原因,一个简单的@synthesize suit; will default to naming the ivar suit without the underscore prefix. 将默认命名为没有下划线前缀的ivar suit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM