解析从数组到UILabel的文本

时间:2010-02-24 16:31:21

标签: objective-c nsarray uilabel

我实际上已经开始失去活着的意志,这段代码让我疯了!

我正在尝试将mathspractice.txt的内容放入* myLabel

我正在使用一个数组:

-(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];
}

然后我试图将它传递给* myLabel

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

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

它会快速检查您的theMathsPractice是NSArray,而不是NSString,这是您要分配给标签的text属性的内容。在将数组分配给标签之前,您至少应该将该数组格式化为某种字符串。

(也不确定为什么你在赋值中使用*取消引用它 - 我认为这会引发编译器错误,因为实际上不允许裸露的非引用Objective-C对象。)

答案 1 :(得分:0)

我会使用以下内容:

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