开发者

NSARRAY Coding error

开发者 https://www.devze.com 2022-12-22 19:25 出处:网络
Can anyone notice an error in this coding??? NSString *textFilePath = [[NSBundle mainBundle] pathForResource:@\"test\" ofType:@\"txt\"];

Can anyone notice an error in this coding???

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];

I'm trying to pass text from text.txt into a label on a scrollview...Its shows no errors when it compiles...

Thanks in advance


practiceContent = [fileContents componentsSeparatedByString:@" "]; 
...
lblText.text = practiceContent;

practiceContent is an NSArray, but lblText.text requires an NSString. You should simply write

lblText.text = fileContents;

The reason the compiler doesn't complain is probably you've declared practiceContent as an id. The compiler cannot perform compile-time type check if the type is id.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号