我正在尝试创建一个游戏,其中的人们回答问题。我正在从文本文件加载问题。不同部分之间用?和问题分开。分离的代码工作正常,除了当我试图用它创建一个问题。拆分NSString后获取EXC_BAD_ACCESS
for(int n = 0; n<[questionsFromFile count]; n++)
{
NSArray *params = [((NSString *)questionsFromFile[n]) componentsSeparatedByString:@"•"];
NSString *fact = params[0];
NSString *prompt = params[1];
NSString *image = params[2];
NSString *answerAsString = params[3];
BOOL answer = [answerAsString boolValue];
YNQuestion *question = [[YNQuestion alloc]initWithFact:fact prompt:prompt image:image answer:answer];
[self.allQuestions addObject:question];
}
在questions.txt:
fact•prompt•unknown.png•YES
§fact•prompt•unknown.png•NO
当我运行它,直到问题被加载它工作正常。然后,它与EXC_BAD_ACCESS(code=2)
崩溃。如果我将事实,提示,图像等替换为“你好”或@“再见”,它可以正常工作。
我正在使用ARC。另外,这里是YNQuestion
的代码。
@interface YNQuestion : NSObject
@property(assign, nonatomic)NSString *fact;
@property(assign, nonatomic)NSString *prompt;
@property(assign, nonatomic)NSString *image;
@property(assign, nonatomic)BOOL answer;
-(id)initWithFact:(NSString *)fact prompt:(NSString *)prompt image:(NSString *)image answer: (BOOL) answer;
-(BOOL)checkIfCorrect: (BOOL)answer;
@end
现在,它的工作。只适用于那些不是我的默认设置。
惊喜!它不再工作。我相信错误是在.txt文件中有硬编码的答案和答案。我在测试。
请在'YNQuestion'中发布实例变量/属性的头部定义。你还使用ARC还是手动引用计数? –
@JasonHarwig我刚刚做到了。 – joeelectricity