Why can’t I do
NSLog(@"mySet count: %@", [mySet count]);
and use the NSSet afterwards? (if I do use it the app crashes with “EXC_BAD_ACCESS”
I’ve tried mutable/immutable, retained, copied, autoreleased… all kinds of sets, And after the count message they all become useless and crash. And if I don’t count they behave as expected, And if I print them out like this:
NSLog(@"mySet: %@", mySet);
it’s fine! the problem seems to be just with count.
can someone explain me this? I’ve searched and found nothing regarding this strange behaviour
The reason this crashes is because
[mySet count]returns an integer, and the%@format specifier expects an object. Change it to:This is one of the “gotchas” of Objective C (unless you are already familiar with C or C++): sometimes your data is an object and sometimes your data is not an object. This is necessary for compatibility with C code.