任何人都可以注意到这个编码中的错误吗?
NSString *textFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:NULL];
practiceContent = [fileContents componentsSeparatedByString:@" "];
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myScrollView.contentSize = CGSizeMake(320,960);
myScrollView.pagingEnabled = FALSE;
myScrollView.scrollEnabled = TRUE;
myScrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myScrollView];
UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(0,100,960,40)];
lblText.text = practiceContent;
[myScrollView addSubview:lblText];
[lblText release];
我正在尝试将text.txt中的文本传递到scrollview上的标签中...它在编译时没有显示错误...
提前致谢
答案 0 :(得分:1)
practiceContent = [fileContents componentsSeparatedByString:@" "];
...
lblText.text = practiceContent;
practiceContent
是NSArray,但lblText.text
需要NSString。你应该写一下
lblText.text = fileContents;
编译器没有抱怨的原因可能是您已将practiceContent
声明为id
。如果类型为id
,则编译器无法执行编译时类型检查。