2
votes

I have one NSMutableArray called SubList that contains 262 element. Every object is a NSDictionary!

This code works like a charm!

for (NSDictionary *element in listSub){
    [cell.textLabel setText:[element objectForKey:@"title"]];  

}  

But if I try to use use this code I get a SIGBART error!

NSDictionary * element = [listSub objectAtIndex:[indexPath row]];

[cell.textLabel setText:[element objectForKey:@"title"]];

So what's the problem?

*EDit the problem is at this line

NSDictionary * element = [listSub objectAtIndex:[indexPath row]];

and this is the description Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x89a8d90' Thanks

**Edit This is the output of listSub!

listSub: {(
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Web Programming";
                label = "Web Programming";
            }
        );
        firstitemmsec = 1265885595862;
        id = "feed/http://feeds.feedburner.com/FamousBloggers";
        sortid = A11327DE;
        title = "Famous Bloggers";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Hacking";
                label = Hacking;
            }
        );
        firstitemmsec = 1241258413829;
        htmlUrl = "http://backtrack-italia.blogspot.com/";
        id = "feed/http://feeds2.feedburner.com/BackTrackItalia";
        sortid = E8A04F76;
        title = "Back Track Italia";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Hacking";
                label = Hacking;
            }
        );
        firstitemmsec = 1245376992188;
        htmlUrl = "http://www.offensive-security.com";
        id = "feed/http://www.offensive-security.com/blog/feed/";
        sortid = 92F57555;
        title = "BackTrack Information Security Distribution";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/iOS Developer";
                label = "iOS Developer";
            }
        );
        firstitemmsec = 1296830392306;
        htmlUrl = "http://amix.dk/Main/";
        id = "feed/http://feeds.feedburner.com/amixdk";
        sortid = 110C52C3;
        title = "amix.dk blog";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Apple News";
                label = "Apple News";
            }
        );
        firstitemmsec = 1285261350202;
        htmlUrl = "http://www.appletvhacks.net";
        id = "feed/http://www.appletvhacks.net/feed/";
        sortid = 81125D2E;
        title = "Apple TV Hacks";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Apple News";
                label = "Apple News";
            }
        );
        firstitemmsec = 1293230300220;
        htmlUrl = "http://www.appletvitalia.it";
        id = "feed/http://www.appletvitalia.it/feed/";
        sortid = 892FE61C;
        title = "Apple Tv Italia";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Apple News";
                label = "Apple News";
            }
        );
        firstitemmsec = 1270115980935;
        htmlUrl = "http://www.appleecious.com";
        id = "feed/http://feeds2.feedburner.com/appleecious";
        sortid = 00B5AFC2;
        title = Appleecious;
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Hacking";
                label = Hacking;
            }
        );
        firstitemmsec = 1258495136927;
        htmlUrl = "http://www.rawseo.com/news";
        id = "feed/http://www.rawseo.com/news/feed/";
        sortid = D6766911;
        title = "A blend of programming and seo";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/Seo e Web Marketing";
                label = "Seo e Web Marketing";
            }
        );
        firstitemmsec = 1233684720758;
        htmlUrl = "http://it-adsense.blogspot.com/";
        id = "feed/http://it-adsense.blogspot.com/atom.xml";
        sortid = 9FB570ED;
        title = "AdSense Blog-Italiano";
    },
        {
        categories =         (
        );
        firstitemmsec = 1277627346000;
        htmlUrl = "http://aext.net";
        id = "feed/http://feeds.feedburner.com/aextnet";
        sortid = 70800CFE;
        title = "AEXT.NET NET MAGAZINE";
    },
        {
        categories =         (
        );
        firstitemmsec = 1217001547735;
        htmlUrl = "http://www.alessandroscoscia.it";
        id = "feed/http://feeds.feedburner.com/alessandroscoscia";
        sortid = 51CB8E6E;
        title = "Alessandro Scoscia";
    },
        {
        categories =         (
                        {
                id = "user/14761688561595773457/label/iOS Developer";
                label = "iOS Developer";
            }
        );
...

I'm sure that it isn't a NSSet.

Now I post the code that I'm using to populate my array.

 NSError *error;
    NSURLResponse *response;
    NSData  *dataReply = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
    NSString *string = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
    NSLog(@"testing %@",string);


    SBJsonParser *vol= [[SBJsonParser alloc]init];
    if (string) {
        NSArray *feeds = [vol objectWithString:string error:nil];
        NSDictionary *results = [string JSONValue];
        NSArray *subs = [results valueForKey:@"subscriptions"];

        for (NSDictionary *iscrizione in subs){

            [subscriptions addObject:iscrizione];

        }

This is part of another class and also in this part of my code if i try to do something like this: NSDictionary * element = [listSub objectAtIndex:0]; NSLog([element objectForKey:@"title"]);

I get the same error while if I try a loop like this

for (NSDictionary *element in listSub){
    [cell.textLabel setText:[element objectForKey:@"title"]];  

} 

Everythings works fine without NSSet errors. This make me crazy!

5
Can you paste the error message also... - Robin
what is lol, you are calling [lol objectForKey:@"title"] and get right result,While having dictonary in element? - Ishu
Have you any % in [element objectForKey:@"title"]? - Jilouc
check updated one.call [listSub retain] where you access data on it. - Ishu
I added [listSub retain] before the NSDictionary * element = [listSub objectAtIndex:[indexPath row]]; but doesn't work :( - paul_1991

5 Answers

2
votes

The problem is probably that listSub was not properly retained and therefore released already. The memory is now re-used and the object at that address is now an __NSCFSet, which does not respond to objectAtIndex:.

Note that autoreleased objects returned by a convenience class method like [NSMutableArray arrayWithXXX] should be retained and only released when not needed anymore. The best way to be sure they are retained is to declare a @property(retain) NSArray *subList, @synthesize it and use self.subList in all cases except perhaps in dealloc.

Addition

It was suggested that the method that created subList returned a set and not an array. If that were the case, the simple loop at the begining of the answer would not function either. That is why I find it very unlikely that subList was originally an NSSet, and much more likely that the array was overreleased and dealloced and the memory re-used for the NSSet.

Please do what people who want to help you ask of you.

Add the line

NSLog(@"listSub: %@", listSub);

at the place where listSub is set and also right before the error line. Then look in the error log what it displays and tell us.

1
votes

Have you tried:

[cell.textLabel setText:[element objectForKey:@"title"]];  

What is that "lol"??

1
votes

listSub isn't an array, it's an NSSet.

0
votes

problem is here

[cell.textLabel setText:[NSString stringWithFormat:[element objectForKey:@"title"]]];

when you are call stringWithFormat then you need to give format in string literal form like this

[cell.textLabel setText:[NSString stringWithFormat:@"%@",[element objectForKey:@"title"]]];

or direct set the text with out calling stringWithFormat:

your array got released some where, so for solving the problem you need to call retain on tyour array at right place.

0
votes

Just try this one.

NSDictionary * element = [listSub objectAtIndex:[indexPath row]];
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[element objectForKey:@"title"]]];

Or try this as you have edit your question, Edit

NSDictionary *element = [[NSDictionary alloc] initWithDictionary:[listSub objectAtIndex:indexPath.row]];
[cell.textLabel setText:[element objectForKey:@"title"]];
[element release];