parsing txt from array to UILabel

匆匆过客 提交于 2019-12-13 03:54:01

问题


I'm actually starting to loose the will to live, this piece of code is driving me nuts!

I'm trying to get the content of mathspractice.txt into *myLabel

I'm using an array which is:

-(void)loadText
{
    NSArray *wordListArray = [[NSArray alloc] initWithArray:
    [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”mathspractice” ofType:@”txt”]
    encoding:NSMacOSRomanStringEncoding error:NULL] componentsSeparatedByString:@”\n”]];
    self.theMathsPractice = wordListArray;
    [wordListArray release];
}

and then I'm trying to pass it into *myLabel

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,960,40)];
        myLabel.text = *theMathsPractice;
        [myScrollView addSubview:myLabel];
        [myLabel release];
        }

Can anyone help?


回答1:


It looks on quick inspection that your theMathsPractice is an NSArray, not an NSString, which is what you'd want to assign to the label's text property. You should at least format that array back into a string of some sort before assigning it to the label.

(Also not sure why you're dereferencing it with the * in the assignment-- I would think that would throw a compiler error, since naked non-reference Objective-C objects are not really allowed.)




回答2:


I would use the following:

myLable.text = [theMathsPractice componentsJoinedByString:@" "]);


来源:https://stackoverflow.com/questions/2327656/parsing-txt-from-array-to-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!