2010-02-25 46 views
0

我已经查看过虽然以前的问题和对这个错误的答案,但我无法弄清楚我做错了什么。谁能帮忙?警告:从不同的Objective-C类型分配

@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 
+0

你可以把调试器的日志吗? – 2010-02-25 13:54:58

+0

我从调试器中得到的所有信息都是“会话开始”,它实际上编译得很好,只有一个警告,但它没有显示我的文本mathspractice.txt – 2010-02-25 14:15:52

+0

我需要IB中的标签吗? – 2010-02-25 14:17:13

回答

1

我猜mathsPracticeTextArray被声明为NSArray*NSMutableArray*,在这种情况下分配一个NSString*它(在-(void)loadText发生)会导致你在标题中提到的警告。

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

+0

这样? - (void)loadText { \t NSBundle * bundle = [NSBundle mainBundle]; \t NSString * textFilePath = [bundle pathForResource:@“mathspractice”ofType:@“txt”]; \t NSArray * fileContents = [NSString stringWithContentsOfFile:textFilePath]; \t mathsPracticeTextArray = fileContents; \t } – 2010-02-25 14:46:25

+0

代码在评论中永远不会有效;太乱了。改变'fileContents'的类型只会将问题转移到另一条线上,因为它现在将一个字符串存储为一个数组。请看“NSArray”的方法(http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html)。另外,不赞成使用'stringWithContentsOfFile:'(http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSString_Class/DeprecationAppendix/AppendixApresentedAcpreferredAPI.html#//apple_ref/doc/uid/20000154 -stringWithContentsOfFile_)。 – outis 2010-02-25 14:54:13

相关问题