0

I am having an this error come up:

No visible @interface for 'BinaryFileReader' declares the selector 'initWithLocation:'

for every method called on BinaryFileReader in the method below.

+ (Item*)loadItem:(NSString*)filepath {
  Item* newItem = [[Item alloc] init];
  BinaryFileReader* input = [[BinaryFileReader alloc] initWithLocation:filepath];
  [newItem setName:[input readNSString]];
  [newItem setOtherNames:[input readNSMutableArrayOfNSString]];
  [newItem setDescription:[input readNSString]];
  [newItem setXactCode:[input readNSString]];
  [newItem setSymbilityCode:[input readNSString]];
  [newItem setAverageLowPrice:[input readInt]];
  [newItem setAverageHighPrice:[input readInt]];
  [newItem setAverageLifeExpectancy:[input readInt]];
  return newItem;
}

Here is my BinaryFileReader.m:

#import "BinaryFileReader.h"

@implementation BinaryFileReader

- (id)init {
  self = [super init];
  return self;
}

- (id)initWithLocation:(NSString*)filepath {
  if ((self = [super init])) {
      _file = [NSFileHandle fileHandleForReadingAtPath:filepath];
      _fileOffset = 0;
      if (_file == nil)
          NSLog(@"%@%@",@"Failed to open file at path:",filepath);
  }
  return self;
}

- (void)close {
  [_file closeFile];
}

- (int)readInt {
  [_file seekToFileOffset:_fileOffset];
  _databuffer = [_file readDataOfLength:4];
  _fileOffset+=4;
  return (*(int*)([_databuffer bytes]));
}
- (NSString*)readNSString {
  int length = [self readInt];
  [_file seekToFileOffset:_fileOffset];
  _databuffer = [_file readDataOfLength:length];
  _fileOffset+=length;
  return [[NSString alloc] initWithData:_databuffer encoding:NSUTF8StringEncoding];
}

- (NSMutableArray*)readNSMutableArrayOfNSString {
  NSMutableArray* array = [[NSMutableArray alloc] init];
  int arrayLength = [self readInt];
  int length;
  for (int i=0; i<arrayLength; i++) {
      length = [self readInt];
      [_file seekToFileOffset:_fileOffset];
      _databuffer = [_file readDataOfLength:length];
      _fileOffset+=length;
      [array addObject:[[NSString alloc] initWithData:_databuffer encoding:NSUTF8StringEncoding]];
  }
  return array;
}

@end

And BinaryFileReader.h:

@interface BinaryFileReader : NSObject

@property (nonatomic) int fileOffset;
@property (nonatomic, retain) NSData* databuffer;
@property (nonatomic, retain) NSFileHandle* file;

- (id)init;
- (id)initWithLocation:(NSString*)filepath;
- (void)close;
- (int)readInt;
- (NSString*)readNSString;
- (NSMutableArray*)readNSMutableArrayOfNSString;

@end

I don't understand why I am having this issue. Any help to clear this up would be much appreciated.

4

1 回答 1

0

我将您问题中的代码复制并粘贴到我的项目中。预期结果:一切正常。因此,问题绝对不是您的代码。

回到另一个BinaryFileReader.h污染你的#imports的想法:你在你的项目中使用任何第三方库吗?如果是,其中一个可能会意外提供BinaryFileReader.h文件。

试试这个:重命名BinaryFileReader.h为其他名称,例如Foo.h. 还要将 #import in 更改BinaryFileReader.m为 use Foo.h,但将 #import 保留在loadItem:方法所在的文件中,以便它仍然使用BinaryFileReader.h. 现在再次编译。编译器现在应该抱怨它找不到BinaryFileReader.h. 如果错误仍然与缺少的选择器有关,那么您就知道在某处您意外地BinaryFileReader.h弄乱了您的项目。

于 2013-06-11T21:57:12.090 回答