我已经创建了一个单独的类'ArrayManager',它听起来很像,只是用于保存和保留不会消失的数据,同时对许多其他类可见。 NSMutableArray * cellArray按预期工作,没问题。
为方便起见,我(尝试)将类“MapProps”的指针添加到“Array Manager”类中。我的想法只要我只是保持一个指针保留在那里应该没有问题,但主发誓没有可见的界面。
我仍然是Objective-c的新手,所以也许我正在进行中,这只是一个语法错误,或者我可能完全偏离了基础。有些人可以提供一些反馈,说明为什么会这样吗?
//MapProps.h
#import
@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];
这里我收到消息'no visible @interfor ArrayManager ....
}
return 0;
}
现在,是的,还有其他方法可以做我想要的事情。但在这种情况下,我真的很想了解并创建最好的代码。
答案 0 :(得分:2)
这与类之间的关系无关。您需要将ArrayManager的头文件导入main.m,否则编译器不知道该类是否存在或者它定义的方法。