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;
}
现在,是的,还有其他的方法做什么我想在这里。但在这种情况下,我真的很想理解并创建最好的代码。
实际上它已经是,我想我应该已经张贴在,但我的问题表示,它正在工作,不然,就不是从“MapProps”类的方法。 – user1615285