2012-09-20 38 views
0

我創建了一個單獨的類'ArrayManager',它聽起來非常類似,只是爲了保存和保留一定不會消失的數據,而對許多其他類可見。 NSMutableArray * cellArray按預期工作,沒有問題。我應該能夠添加一個類到單身人士嗎?

爲了方便起見,我(嘗試)的指針添加到類「MapProps」到類「陣列管理器」。我的想法是,只要我保留一個指針應該沒有問題,但主發誓它沒有可見的接口。

我還是新來的Objective-C,也許我上了軌道,它只是一個語法錯誤,或者是我完全關閉基地。有些人可以給出一些反饋,說明爲什麼會發生這種情況? //MapProps.h
#進口

@interface MapProps : NSObject{ 
    int pixelsX; 
    int pixelsY; 
    int tileSize; 
    int rowsMax; 
    int columnsMax; 
}; 
@property int pixelsX,pixelsY,tileSize,rowsMax,columnsMax; 
-(void)setPixelsX:(int)x andPixelsY:(int)y withTilesize:(int)tiles; 
-(void)displayValues; 
@end 


//MapProps.m 
@implementation MapProps 
@synthesize pixelsX,pixelsY,tileSize,rowsMax,columnsMax; 

-(void) setPixelsX:(int)x andPixelsY:(int)y withTilesize:(int)tiles { 
    [self setPixelsX:x]; 
    [self setPixelsY:y]; 
    [self setTileSize:tiles]; 
    [self setRowsMax:(pixelsX/tileSize)]; 
    int yScalar = (.875 * tileSize); 
    [self setColumnsMax:(pixelsY/yScalar)]; 
    [self displayValues]; 
}; 

-(void)displayValues { 
    NSLog(@"PixelsX=%d PixelsY=%d ",pixelsX,pixelsY); 
    NSLog(@"TileSize=%d",tileSize); 
    NSLog(@"RowsMax=%d ColumnsMax=%d",rowsMax,columnsMax); 
}; 
@end 




//ArrayManager.h 
#import <Foundation/Foundation.h> 
@class MapProps; // include map properties 

@interface ArrayManager : NSObject{ 
    NSMutableArray *cellArray; 
    MapProps *mapProps; 
} 
@property (nonatomic,retain) NSMutableArray *cellArray; 
@property (nonatomic,retain) MapProps *mapProps; 


+(id)sharedArrayManager; 
-(void)setIntAtIndex:(int)index withValue:(int)value; 
-(int)getIntAtIndex:(int)index; 
@end 


#import "ArrayManager.h" 
#import "MapProps.h" 

@implementation ArrayManager 
@synthesize cellArray; 
@synthesize mapProps; 


+(id)sharedArrayManager{ 
    static id sharedArrayManager = nil; 

    if (!sharedArrayManager){ 
     sharedArrayManager = [[self alloc]init]; 
    } 
    return sharedArrayManager; 
} 

-(id)init{ 
    self = [super init]; 
    if (self){ 
     mapProps = [[MapProps alloc]init]; 
     cellArray=[[NSMutableArray alloc]init]; 

     for (int i=0; i <=600; i++) { 
      [cellArray addObject:[NSNumber numberWithInt:i]]; 
     }; 

    } 
    return self; 
}; 

-(void)setIntAtIndex:(int)index withValue:(int)value{ 
    [cellArray replaceObjectAtIndex:index withObject:[NSNumber numberWithInt:value]]; 
}; 

-(int)getIntAtIndex:(int)index{ 
    return ([[cellArray objectAtIndex:index] intValue]); 
}; 
@end 




//Main 
int main(int argc, const char * argv[]) 
{ 

    @autoreleasepool { 
     int x; 
     ArrayManager *arrayManager = [ArrayManager sharedArrayManager]; 

     [arrayManager.cellArray replaceObjectAtIndex:1 withObject:[NSNumber    numberWithInt:5]]; 
     x = [[arrayManager.cellArray objectAtIndex:1] intValue]; 
     NSLog(@"you got %d",x); 

     [arrayManager setIntAtIndex:2 withValue:2]; 
     x = [arrayManager getIntAtIndex:2]; 

     NSLog(@"you got %d",x); 

    [arrayManager setPixelsX:320 andPixelsY:480 withTilesize:16]; 

在這裏,我得到的消息「沒有@interfor ArrayManager可見....

} 
    return 0; 
} 

現在,是的,還有其他的方法做什麼我想在這裏。但在這種情況下,我真的很想理解並創建最好的代碼。

回答

2

這與類之間的關係無關。您需要將ArrayManager的頭文件導入到main.m中,否則編譯器不知道該類存在或其定義的方法。

+0

實際上它已經是,我想我應該已經張貼在,但我的問題表示,它正在工作,不然,就不是從「MapProps」類的方法。 – user1615285

相關問題