views:

133

answers:

1

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);
    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  A: 

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.

outis
like this?-(void)loadText{ NSBundle *bundle = [NSBundle mainBundle]; NSString *textFilePath = [bundle pathForResource:@"mathspractice" ofType:@"txt"]; NSArray *fileContents = [NSString stringWithContentsOfFile:textFilePath]; mathsPracticeTextArray = fileContents; }
Charles Marsh
Code never works well in comments; too messy. Changing the type of `fileContents` merely moves the problem to a different line, since it's now storing a string as an array. Look to the methods of `NSArray` (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html). Also, `stringWithContentsOfFile:` is deprecated (http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSString_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/doc/uid/20000154-stringWithContentsOfFile_).
outis