2014-12-11 1 views
0

У меня возникли проблемы с доступом к переменной «megaBubbleUpgradeTier» из моего класса FirstViewController.m. Я пытаюсь получить доступ к нему здесь:Переменная недоступна, импорт предоставляется в Xcode

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 
+0

Что такое точные сообщения об ошибках Xcode? – dpassage

+0

@dpassage Свойство 'megaBubbleUpgradeTier' не найдено на объекте типа 'RWGameData *' – user4334739

+0

Возможно, это просто икота Xcode. См. Ответы на [Свойство x не найдено на объекте типа y ошибка только с ARC] (http://stackoverflow.com/q/10527653) –

ответ

0

ли вы помните, чтобы добавить строку

#import "RWGameData.h" 

В верхней части исходного файла? Если вы не импортируете заголовок, компилятор не знает об этом классе и подумает, что в этом классе не найдено ВСЕ.

EDIT:

Попробуйте изменить эту строку:

+(instancetype)sharedGameData; 

Чтобы прочитать

+(RWGameData *)sharedGameData; 

Я держал пари, что фиксирует ее.

+0

RWGameData.h доступен. Я могу ссылаться на [RWGameData sharedGameData], это просто то, что в этой конкретной функции я не могу ссылаться на переменные, которые связаны с классом RWGameData. – user4334739

+0

Вы импортировали заголовок в верхней части вашего .m-файла? –

+0

Я только что проверил и сделал. : | – user4334739

0

Я создал новый проект и передал его вручную. Я могу только предположить, что что-то не так на конце Xcode, что было предложено ранее JoshCaswell, хотя я изо всех сил старался очистить проект, но я, должно быть, что-то пропустил.