gpt4 book ai didi

ios - 如何通过 ios 中的继承访问私有(private)声明的属性

转载 作者:行者123 更新时间:2023-11-28 22:10:16 42 4
gpt4 key购买 nike

我不明白为什么我不能访问子类 SetCardCardgameViewController 中的 self.cardButtons。父级是 CardgameViewController。

CardgameViewController.h:

 #import <UIKit/UIKit.h>
#import "Deck.h"

@interface CardgameViewController : UIViewController
- (void) updateUI;

// protected
// for subclasses

- (Deck *)createDeck;

// must be overriden in SetCardGameViewController as contents is not to be used for instance
- (NSAttributedString *)titleForCard: (Card *)card;
- (UIImage *)backgroundImageforCard: (Card *)card;

@end

CardgameViewController.m

#import "CardgameViewController.h"
//#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"

@interface CardgameViewController ()

@property (strong, nonatomic) CardMatchingGame *game;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UISegmentedControl *matchMode;
@property (weak, nonatomic) IBOutlet UILabel *status;
@property (weak, nonatomic) IBOutlet UISlider *historySlider;
@property (weak, nonatomic) IBOutlet UILabel *historyOverview;

@end

SetCardCardgameViewController.h

#import "CardgameViewController.h"

@interface SetCardCardgameViewController : CardgameViewController

@end

SetCardCardgameViewController.m

#import "SetCardCardgameViewController.h"
#import "SetCardDeck.h"
#import "SetCard.h"

@interface SetCardCardgameViewController ()

@end

@implementation SetCardCardgameViewController

- (void)updateUI
{
for (UIButton *cardButton in self.cardButtons) {

}

}

无法识别最后一个 self.cardButtons。但是 cardButtons 是父类的一个属性。为什么不被认可?我同意它是私下声明的,但由于 SetCardCardgameViewController 是 CardgameViewController 的子项,我认为我可以访问它的所有属性和方法。还是我弄错了?

最佳答案

您将私有(private)范围误认为是 protected 。

从语义上讲,由于 Objective-C 的动态特性,不存在私有(private)或 protected 范围之类的东西。您可以按照上面的方式模拟私有(private)范围;但是,在 .m 文件中声明的方法或属性不能被其子类看到。将它们放在您的头文件中会使它们公开,并且通常是最常用的途径。如果你绝对必须保护这些方法/属性,你可以将该接口(interface)添加到一个单独的头文件中,然后将它们导入你的子类中,如下所示:

CardgameViewController_Internal.h

@interface CardgameViewController (Private)

@property (strong, nonatomic) CardMatchingGame *game;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UISegmentedControl *matchMode;
@property (weak, nonatomic) IBOutlet UILabel *status;
@property (weak, nonatomic) IBOutlet UISlider *historySlider;
@property (weak, nonatomic) IBOutlet UILabel *historyOverview;

@end

然后在 SetCardCardgameViewController.m 中:

#import "SetCardCardgameViewController.h"
#import "SetCardDeck.h"
#import "SetCard.h"
#import "CardgameViewController_Internal.h"

@implementation SetCardCardgameViewController

- (void)updateUI
{
for (UIButton *cardButton in self.cardButtons) {

}
}

@end

关于ios - 如何通过 ios 中的继承访问私有(private)声明的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23093607/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com