warning: assignment from distinct Objective-C type
I've looked though al the previous questions and answers on this error but I can't figure out what I'm doing wrong. Can anyone help?
@synthesize myScrollView;
@synthesize mathsPracticeTextArray;
-(void)loadText
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *textFilePath = [bundle pathForResource:@"mathspractice" ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:textFilePath];
mathsPracticeTextArray = fileContents;
}
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myScrollView.contentSize = CGSizeMake(320, 960);
开发者_如何学PythonmyScrollView.pagingEnabled = FALSE;
myScrollView.scrollEnabled = TRUE;
myScrollView.backgroundColor = [UIColor whiteColor];
*[self.view addSubview:myScrollView];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,960,40)];
myLabel.text = [mathsPracticeTextArray componentsJoinedByString:@" "];
[myScrollView addSubview:myLabel];
[myLabel release];*
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[myScrollView release];
[mathsPracticeTextArray release];
[super dealloc];
}
@end
I'm guessing mathsPracticeTextArray
is declared as an NSArray*
or NSMutableArray*
, in which case assigning an NSString*
to it (as happens in -(void)loadText
) will cause the warning you mention in your title.
The warning is quite a clue what's happening: NSString is distinct from NSArray, and you can't treat one as the other. As you're assigning the wrong type of object to the variable, many messages you send to the object can't be handled and your app will fail.
精彩评论