user1010563
user1010563

Reputation:

NSLog shows NSString correctly - Trying to show the string on a label crashes the app (unrecognized selector)

App crashes when trying to update an UILabel with a NSString. Showing the same NSString on console works.

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

Upvotes: 1

Views: 117

Answers (3)

Tony Stark
Tony Stark

Reputation: 172

maybe the values you stored in self.matchesArray is not a string

Upvotes: 0

Rushi
Rushi

Reputation: 4500

Try :

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

Upvotes: 0

Bryan Chen
Bryan Chen

Reputation: 46598

title is not NSString, it is NSArray

so

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

Upvotes: 2

Related Questions