警告:从不同的Objective-C类型分配

时间:2010-02-25 13:45:50

标签: objective-c nsarray compiler-warnings

我已经查看过这个错误的所有先前的问题和答案,但我无法弄清楚我做错了什么。有人可以帮忙吗?

@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);
    myScrollView.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

1 个答案:

答案 0 :(得分:1)

我猜mathsPracticeTextArray被声明为NSArray*NSMutableArray*,在这种情况下为NSString*分配-(void)loadText(如{{1}}中所示)会引起你在标题中提到的警告。

警告非常清楚发生了什么:NSString与NSArray不同,你不能把它当作另一个。当您将错误类型的对象分配给变量时,您发送给对象的许多消息都无法处理,您的应用程序将失败。