Xcode中提供的变量无法访问,导入

时间:2014-12-11 20:14:37

标签: ios objective-c xcode

我无法从FirstViewController.m类中访问变量“megaBubbleUpgradeTier”。我试图在这里访问它:

FirstViewController.m

#import "FirstViewController.h"
#import "customTableCell.h"
//#import "RWGameData.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
{
    NSArray *upgrades;
    NSArray *thumbnails;
    NSArray *descriptions;
    NSArray *megaBubbleUpgradeFees;
    NSInteger rowID;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Upgrades" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

    upgrades = [dict objectForKey:@"UpgradeStrings"];
    thumbnails = [dict objectForKey:@"UpgradeImages"];
    descriptions = [dict objectForKey:@"UpgradeDescriptions"];
    megaBubbleUpgradeFees = [dict objectForKey:@"MegaBubbleUpgradeFee"];

    _regularBubbleLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].regularBubbleCount];
    _premiumBubbleLabel.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].premiumBubbleCount];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [upgrades count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Handles appearance of cells in table.

    static NSString *TableIdentifier = @"TableCell";

    customTableCell *cell = (customTableCell *)[tableView dequeueReusableCellWithIdentifier:TableIdentifier];

    if (cell == nil) {
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.primaryImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.upgradeNameLabel.text = [upgrades objectAtIndex:indexPath.row];
    cell.descriptionLabel.text = [descriptions objectAtIndex:indexPath.row];
    cell.regularCurrencyIcon.image = [UIImage imageNamed:@"megaBubbleLarge30.png"];

    cell.regularBubbleCostLabel.text = [NSString stringWithFormat:@"%@", megaBubbleUpgradeFees[[RWGameData sharedGameData].megaBubbleUpgradeTier]];


    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    rowID = indexPath.row;
    [self makePayment:self];
}

- (IBAction)makePayment:(id)sender {
    UIAlertView *messageAlert;

    if (rowID == 0) {
        messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil];
        [messageAlert show];
        NSLog(@"Cell ID: 0");
    } else if (rowID == 1) {
        messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil];
        [messageAlert show];
        NSLog(@"Cell ID: 1");
    } else if (rowID == 2) {
        messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil];
        [messageAlert show];
        NSLog(@"Cell ID: 2");
    } else if (rowID == 3) {
        messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil];
        [messageAlert show];
        NSLog(@"Cell ID: 3");
    } else if (rowID == 4) {
        messageAlert = [[UIAlertView alloc] initWithTitle:@"Not enough bubbles!!" message:@"You need to collect more bubbles or purchase them from our store!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", nil];
        [messageAlert show];
        NSLog(@"Cell ID: 4");
    }
}

@end

问题是[RWGameData sharedGameData] .megaBubbleUpgradeTier似乎无法从类中访问。有什么想法吗?如果需要,我可以提供更多信息。声明变量的#RWGameData.h已经在FirstViewController头文件中导入,因此它应该可以在相应的.m文件FirstViewController.m中访问。

显示的错误是:

Property 'megaBubbleUpgradeTier' not found on object of type 'RWGameData *'

FirstViewController.h

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

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UILabel *regularBubbleLabel;
@property (strong, nonatomic) IBOutlet UILabel *premiumBubbleLabel;

@property (strong, nonatomic) IBOutlet UIImageView *regularBubbleIcon;
@property (strong, nonatomic) IBOutlet UIImageView *premiumBubbleIcon;

@property (strong, nonatomic) IBOutlet UINavigationBar *navBar;

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

RWGameData.h

#import <Foundation/Foundation.h>

@interface RWGameData : NSObject <NSCoding>

@property (assign, nonatomic) long regularBubbleCount;
@property (assign, nonatomic) long premiumBubbleCount;

@property (assign, nonatomic) long megaBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleFactoryUpgradeTier;
@property (assign, nonatomic) long bubblersUpgradeTier;
@property (assign, nonatomic) long mysteryBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleBankUpgradeTier;

+(RWGameData *)sharedGameData;
-(void)reset;
-(void)save;

@end

RWGameData.m

#import "RWGameData.h"

@implementation RWGameData

static NSString* const SSGameDataRegularBubbleCountKey = @"regularBubbleCount";
static NSString* const SSGameDataPremiumBubbleCountKey = @"premiumBubbleCount";

static NSString* const SSGameDataMegaBubbleUpgradeTierKey = @"megaBubbleUpgradeTier";
static NSString* const SSGameDataBubbleFactoryUpgradeTierKey = @"bubbleFactoryUpgradeTier";
static NSString* const SSGameDataBubblersUpgradeTierKey = @"bubblersUpgradeTier";
static NSString* const SSGameDataMysteryBubbleUpgradeTierKey = @"mysteryBubbleUpgradeTier";
static NSString* const SSGameDataBubbleBankUpgradeTierKey = @"bubbleBankUpgradeTier";

+ (RWGameData *)sharedGameData {
    static id sharedInstance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [self loadInstance];
    });

    return sharedInstance;
}

-(void)reset {
    self.regularBubbleCount = 0;
    self.premiumBubbleCount = 0;

    self.megaBubbleUpgradeTier = 0;
    self.bubbleFactoryUpgradeTier = 0;
    self.bubblersUpgradeTier = 0;
    self.mysteryBubbleUpgradeTier = 0;
    self.bubbleBankUpgradeTier = 0;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeDouble:self.regularBubbleCount forKey: SSGameDataRegularBubbleCountKey];
    [encoder encodeDouble:self.premiumBubbleCount forKey: SSGameDataPremiumBubbleCountKey];

    [encoder encodeDouble:self.megaBubbleUpgradeTier forKey: SSGameDataMegaBubbleUpgradeTierKey];
    [encoder encodeDouble:self.bubbleFactoryUpgradeTier forKey: SSGameDataBubbleFactoryUpgradeTierKey];
    [encoder encodeDouble:self.bubblersUpgradeTier forKey: SSGameDataBubblersUpgradeTierKey];
    [encoder encodeDouble:self.mysteryBubbleUpgradeTier forKey: SSGameDataMysteryBubbleUpgradeTierKey];
    [encoder encodeDouble:self.bubbleBankUpgradeTier forKey: SSGameDataBubbleBankUpgradeTierKey];
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    self = [self init];
    if (self) {
        _regularBubbleCount = [decoder decodeDoubleForKey: SSGameDataRegularBubbleCountKey];
        _premiumBubbleCount = [decoder decodeDoubleForKey: SSGameDataPremiumBubbleCountKey];

        _megaBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMegaBubbleUpgradeTierKey];
        _bubbleFactoryUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryUpgradeTierKey];
        _bubblersUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubblersUpgradeTierKey];
        _mysteryBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMysteryBubbleUpgradeTierKey];
        _bubbleBankUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleBankUpgradeTierKey];
    }
    return self;
}

+(NSString*)filePath
{
    static NSString* filePath = nil;
    if (!filePath) {
        filePath =
        [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
         stringByAppendingPathComponent:@"gamedata"];
    }
    return filePath;
}

+(instancetype)loadInstance
{
    NSData* decodedData = [NSData dataWithContentsOfFile: [RWGameData filePath]];
    if (decodedData) {
        RWGameData* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
        return gameData;
    }

    return [[RWGameData alloc] init];
}

-(void)save
{
    NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
    [encodedData writeToFile:[RWGameData filePath] atomically:YES];
}

@end

2 个答案:

答案 0 :(得分:0)

你记得添加一行

吗?
#import "RWGameData.h"

在源文件的顶部?如果你没有导入标题,编译器就不会知道这个类,并且认为在课堂上找不到所有内容。

编辑:

尝试更改此行:

+(instancetype)sharedGameData;

阅读

+(RWGameData *)sharedGameData;

我敢打赌,修复它。

答案 1 :(得分:0)

我创建了一个新项目并手动转移了代码。我只能假设Xcode的结局出了问题,这就是JoshCaswell之前提出的建议,尽管我尽力清理项目但我肯定错过了一些东西。