Objective-C不创建综合变量 [英] Objective-C Not Creating Synthesized Variables

查看:77
本文介绍了Objective-C不创建综合变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名开始的iOS开发人员,仍然习惯于这种综合变量和XCode自动创建变量和setter/getter方法的概念.我做了很多研究,但找不到解决我所面临问题的答案.

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. 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

我有以下实现类:

#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

我的错误是,每当我使用_suit变量时,我都会从XCode收到一条错误消息:

My error is, whenever I use the _suit variable I get an error from XCode saying:

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

据我了解,_suit由编译器自动生成,我应该能够使用"_suit"访问头文件中定义的"suit"属性.是因为我要覆盖编译器的自动生成的setter和getter方法吗?将"_suit"更改为"self.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". Is it because I'm overriding the compiler's automatically generated setter and getter methods? 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. Any insight to this would be greatly appreciated, thanks!

推荐答案

如果您为@property手动创建两个访问器(设置器和获取器),则编译器会假定您不需要/想要合成它们. -以及相应的实例变量-为您服务.有两种可能的解决方案.自己声明实例变量:

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. There are two possible solutions. Either declare the instance variable yourself:

@implemntation PlayingCard
{
    NSString *_suit;
}

或者,作为我的首选方法,在自定义访问器上方使用显式的@synthesize语句来告诉编译器您仍然希望为该属性合成实例变量:

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 suit = _suit;

请注意,= _suit是必需的,因为出于传统原因,简单的@synthesize suit;将默认为不带下划线前缀的ivar suit命名.

Note that the = _suit is necessary because for legacy reasons, a simple @synthesize suit; will default to naming the ivar suit without the underscore prefix.

这篇关于Objective-C不创建综合变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆