NSLog正确显示NSString - 试图在标签上显示字符串会导致应用程序崩溃(无法识别的选择器)

时间:2013-01-16 12:58:29

标签: iphone nsstring console nsarray uilabel

尝试使用UILabel更新NSString时,应用崩溃了。 在NSString上显示相同的console

-(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) 

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

尝试:

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

答案 2 :(得分:0)

您存储在self.matchesArray中的值可能不是字符串