0

我创建了一个单例类“ArrayManager”,它的功能与听起来差不多,只是用于保存和保留不能消失的数据,同时对许多其他类可见。NSMutableArray *cellArray 按预期工作,没有问题。

为方便起见,我(尝试)将指向“MapProps”类的指针添加到“Array Manager”类。我的想法是,只要我只保留一个指针就应该没有问题,但是 Main 发誓没有可见的界面。

我还是 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]; 

在这里,我收到消息'不可见 @interfor ArrayManager....

    }
    return 0;
}

现在,是的,还有其他方法可以做我想做的事。但在这种情况下,我真的很想理解并创建尽可能好的代码。

4

1 回答 1

2

这与类之间的关系无关。您需要将 ArrayManager 的头文件导入 main.m 或编译器不知道该类存在或它定义的方法。

于 2012-09-20T19:28:30.157 回答