Xcodeは私のクラスを含めたくないようです
-
22-07-2019 - |
質問
だから、" MazeCell.h"で宣言されているMazeCellというクラスがあります
#import <Foundation/Foundation.h>
enum {
MazeCellEdgeWall = 0,
MazeCellEdgeGate = 1,
MazeCellEdgeExit = 2
};
typedef NSUInteger MazeCellEdge;
@interface MazeCell : NSObject {
MazeCellEdge left;
MazeCellEdge right;
MazeCellEdge down;
MazeCellEdge up;
NSUInteger drawCount;
NSUInteger row;
NSUInteger column;
}
@property MazeCellEdge left;
@property MazeCellEdge right;
@property MazeCellEdge down;
@property MazeCellEdge up;
@property NSUInteger drawCount;
@property NSUInteger row;
@property NSUInteger column;
- (id)initWithLeft:(MazeCellEdge)newLeft
right:(MazeCellEdge)newRight
up:(MazeCellEdge)newUp
down:(MazeCellEdge)newDown
row:(NSUInteger)newRow
column:(NSUInteger)newColumn;
@end
Xcodeは、&quot; warningのような警告を表示し続けます。「MazeView」は、すべてのメソッドで「-left」&quot;
に応答しない場合があります。おもしろいことに、シミュレータでコードが正常に実行されるのは、XCodeがメソッドを知らないというだけです。
以前に宣言されていなかったため、XCodeで MazeCellEdgeWall
を使用できなくなるまでメッセージを無視することに満足しました(これらの警告とエラーはすべて異なるクラスにあります)。
それで、私は一般にプログラミングを始めたばかりなので見逃したかもしれない露骨なエラーを誰かが見たのではないかと思っていました。
編集:コードは長いため元々含めていませんでしたが、エラーが発生するコードを次に示します。
ここに&quot; MazeCell.m&quot;があります:
#import "MazeCell.h"
@implementation MazeCell
@synthesize left;
@synthesize right;
@synthesize down;
@synthesize up;
@synthesize drawCount;
@synthesize row;
@synthesize column;
-(id) init {
if (self = [super init]) {
right = MazeCellEdgeWall;
up = MazeCellEdgeWall;
left = MazeCellEdgeWall;
down = MazeCellEdgeWall;
drawCount = 0;
}
return self;
}
- (id)initWithLeft:(MazeCellEdge)newLeft
right:(MazeCellEdge)newRight
up:(MazeCellEdge)newUp
down:(MazeCellEdge)newDown
row:(NSUInteger)newRow
column:(NSUInteger)newColumn
{
if (self = [super init]) {
left = newLeft;
right = newRight;
up = newUp;
down = newDown;
drawCount = 0;
row = newRow;
column = newColumn;
}
return self;
}
@end
MazeView.hは次のとおりです。
#import "MazeView.h"
#import "MazeCell.h"
#import "NSMutableArray+Stack.h"
#define kCellSidesSize 80.0
@implementation MazeView
@synthesize maze;
@synthesize controller;
@synthesize interfaceOrientation;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
interfaceOrientation = UIInterfaceOrientationPortrait;
[self setBackgroundColor:[UIColor greenColor]];
[self setUserInteractionEnabled:YES];
[self setMaze:[[Maze alloc] initWithSize:MazeSizeMake(4, 6)]];
}
return self;
}
- (void)setMaze:(Maze *)newMaze {
maze = newMaze;
CGRect newFrame = [self frame];
newFrame.size = CGSizeMake([newMaze size].width * kCellSidesSize,
[newMaze size].height * kCellSidesSize);
[self setFrame:newFrame];
}
- (void)setInterfaceOrientation:(UIInterfaceOrientation)newOrientation {
if (interfaceOrientation != newOrientation) {
interfaceOrientation = newOrientation;
CGRect oldFrame = [self frame];
[self setFrame:CGRectMake(oldFrame.origin.y, oldFrame.origin.x,
oldFrame.size.height, oldFrame.size.width)];
[[self superview] setContentSize:[self frame].size];
}
}
- (void)setController:(UIViewController *)newController {
if (controller != newController) {
controller = newController;
}
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
NSUInteger columns = [[self maze] size].width;
NSUInteger rows = [[self maze] size].height;
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, kCellSidesSize - 2.0);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
BOOL isDrawing = NO;
MazeCell *aCell;
NSMutableArray *aStack = [[NSMutableArray alloc] init];
NSUInteger row = 0;
NSUInteger column = 0;
while (YES) {
aCell = [maze getCellInRow:row andColumn:column ofOrientation:interfaceOrientation];
if (isDrawing) {
CGContextAddLineToPoint(context, row * kCellSidesSize + kCellSidesSize / 2.0,
column * kCellSidesSize + kCellSidesSize / 2.0);
} else {
isDrawing = YES;
CGContextMoveToPoint(context, row * kCellSidesSize + kCellSidesSize / 2.0,
column * kCellSidesSize + kCellSidesSize / 2.0);
}
if ([aCell left] == MazeCellEdgeExit && [aCell drawCount] < 1) {
//Warnings and errors
[aCell setDrawCount:1]; //Warning
column--;
} else if ([aCell right] == MazeCellEdgeExit && [aCell drawCount] < 2) {
//Warnings and errors
[aCell setDrawCount:2]; //Warning
column++;
} else if ([aCell up] == MazeCellEdgeExit && [aCell drawCount] < 3) {
//Warnings and errors
[aCell setDrawCount:3]; //Warning
row--;
} else if ([aCell down] == MazeCellEdgeExit && [aCell drawCount] < 4) {
//Warnings and errors
[aCell setDrawCount:4]; //Warning
row++;
} else if ([aStack count] > 0) {
aCell = [aStack pop];
row = [aCell row]; //Warning
column = [aCell column]; //Warning
isDrawing = NO;
} else {
break;
}
}
CGContextStrokePath(context);
[aStack release];
}
@end
繰り返しますが、これは、私がコード化したことを証明するために提供されています。このプログラムは機能し、前述のように、迷路セルメソッドは実際に機能します。MazeCellEdgeExitを定義していないので、もうコンパイルしないということを除けば、Xcodeは警告を表示します。 、それ以外の場合はコンパイルします。
これは非常に奇妙です。しかし、MazeCell.hファイルとMazeCell.mファイルを複製してMzCell.hとMzCell.mに名前を変更し、MazeCellへのすべての参照をMzCellに置き換えると、このプログラムが実行されることを発見しました。
それは答えよりも多くの質問を開きますが...
解決
すべてが私にとって良さそうです。きれいにしてビルドしてみてください。古くなったオブジェクトファイルが漂っている可能性が高く、コンパイラやリンカーを混乱させます。
setMaze:古い迷路をリリースしないなど、明らかなメモリリークがいくつかありますが、コンパイルを妨げるバグを検出できませんでした。 (間違いなく-initWithFrame:で迷路を割り当てるので、少なくともその1つをリークします。)また、デフォルトのセッターセマンティクスは、「保持」ではなく「割り当て」です。または「コピー」 —この場合、後者の2つの動作のいずれかがより理にかなっているように思われます。 Objective-Cの初心者であることがわかります。これらは批判ではなく建設的なフィードバックを意味します。 :-)
他のヒント
実際に警告を生成しているコードを含めることを怠りますが、どこにいてもMazeCell.hヘッダーファイルをインポートしていないようです。
割り当ておよび初期化するオブジェクト(「MazeView」)が「MazeCell」オブジェクトではない可能性があります。これは、チャックが言及したようにMazeCell.hをインポートしなかったこと、または間違ったクラスで「MazeView」オブジェクトを作成したことが原因である可能性があります。 MazeCellタイプのオブジェクトを作成し、「MazeView」という名前を付けるのが直感的です。
また、Xcodeをテストするための現在のアクセス権はありませんが、typedefが誤って宣言されている可能性があります(または単にその構文スタイルに精通していません)。 typedefの6行をこれで置き換えてみてください。
typedef enum {
MazeCellEdgeWall = 0,
MazeCellEdgeGate = 1,
MazeCellEdgeExit = 2
} MazeCellEdge;
編集: 実装ファイルをインクルードしたばかりなので、MazeView(行7)用であることに気付きましたが、元のヘッダーファイルはMazeCell用です。そのため、現時点では、実際にはMazeCellオブジェクト用のコードを記述していません(または投稿していません)。
また、インスタンス変数を@synthesize '(またはそれらのメソッドを記述)していないのではないかと疑っています。