2 lantianyiming lantianyiming 于 2013.01.17 16:24 提问

在lable中显示string时应用崩溃

在用NSString更新UILabel时应用崩溃了,在console中显示同样的NSString就没问题:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (self.connectionData)
    {
        NSError *error;
        self.dict = [NSJSONSerialization JSONObjectWithData:self.connectionData options:kNilOptions error:&error];
        self.matchesArray = self.dict[@"matches"];

        NSString *title = [self.matchesArray valueForKey:@"title"];
        NSLog(@"NSString TITLE contains: %@", title);
        self.titleLabel.text = title;
    }
}

CONSOLE OUTPUT:

2013-01-16 13:54:08.550 ZEITreisen[3168:c07] NSString TITLE contains: (
    "Mark und Dollar"
)
2013-01-16 13:54:08.552 ZEITreisen[3168:c07] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0xde93850
(lldb) 

2个回答

g989_1314125
g989_1314125   2013.01.17 16:45

试试:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (self.connectionData)
    {
        NSError *error;
        self.dict = [NSJSONSerialization JSONObjectWithData:self.connectionData options:kNilOptions error:&error];
        self.matchesArray = self.dict[@"matches"];

        NSString *title = [self.matchesArray valueForKey:@"title"];
        NSLog(@"NSString TITLE contains: %@", title);
        self.titleLabel.text = [NSString stringWithFormat:@"%@",[title objectAtIndex:0]];
    }
}
love_loveec
love_loveec   2013.01.17 16:58

title 不属于NSString, 它其实是NSArray

所以改成这样:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (self.connectionData)
    {
        NSError *error;
        self.dict = [NSJSONSerialization JSONObjectWithData:self.connectionData options:kNilOptions error:&error];
        self.matchesArray = self.dict[@"matches"];

        NSArray *title = [self.matchesArray valueForKey:@"title"];
        NSLog(@"NSString TITLE contains: %@", title);
        self.titleLabel.text = [title lastObject];
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!