简体   繁体   中英

Can't add or remove object from NSMutableSet

Check it:

- (IBAction)toggleFavorite {
    DataManager *data = [DataManager sharedDataManager];
    NSMutableSet *favorites = data.favorites;

    if (thisEvent.isFavorite == YES) {
        NSLog(@"Toggling off");
        thisEvent.isFavorite = NO;
        [favorites removeObject:thisEvent.guid];
        [favoriteIcon setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];
    }
    else {
        NSLog(@"Toggling on, adding %@", thisEvent.guid);
        thisEvent.isFavorite = YES;
        [favorites addObject:thisEvent.guid];
        [favoriteIcon setImage:[UIImage imageNamed:@"isFavorite.png"] forState:UIControlStateNormal];
    }
    NSLog(@"favorites array now contains %d members", [favorites count]);
}

This is fired from a custom UIButton. The UI part works great--toggles the image used for the button, and I can see from other stuff that the thisEvent.isFavorite BOOL is toggling happily. I can also see in the debugger that I'm getting my DataManager singleton.

But here's my NSLog:

2010-05-13 08:24:32.946 MyApp[924:207] Toggling on, adding 05db685f65e2
2010-05-13 08:24:32.947 MyApp[924:207] favorites array now contains 0 members
2010-05-13 08:24:33.666 MyApp[924:207] Toggling off
2010-05-13 08:24:33.666 MyApp[924:207] favorites array now contains 0 members
2010-05-13 08:24:34.060 MyApp[924:207] Toggling on, adding 05db685f65e2
2010-05-13 08:24:34.061 MyApp[924:207] favorites array now contains 0 members
2010-05-13 08:24:34.296 MyApp[924:207] Toggling off
2010-05-13 08:24:34.297 MyApp[924:207] favorites array now contains 0 members

Worst part is, this USED to work, and I don't know what I did to break it.

--EDIT: By request, my shared data singleton code:

.h

#import <Foundation/Foundation.h>

@interface DataManager : NSObject {
    NSMutableArray *eventList;
    NSMutableSet *favorites;
}

@property (nonatomic, retain) NSMutableArray *eventList;
@property (nonatomic, retain) NSMutableSet *favorites;

+(DataManager*)sharedDataManager;

@end

.m:

#import "DataManager.h"

static DataManager *singletonDataManager = nil;

@implementation DataManager

@synthesize eventList;
@synthesize favorites;

+(DataManager*)sharedDataManager {
    @synchronized(self) {
        if (!singletonDataManager) {
            singletonDataManager = [[DataManager alloc] init];
        }
    }
    return singletonDataManager;
}

- (DataManager*)init {
    if (self = [super init]) {
        eventList = [[NSMutableArray alloc] init];
        favorites = [[NSMutableSet alloc] init];
    }
    return self;
}

------EDIT EDIT EDIT EDIT------

At @TechZen's suggestion, I moved my accessor methods into the data manager singleton. Here's what it now looks like:

#import "DataManager.h"

static DataManager *singletonDataManager = nil;

@implementation DataManager

@synthesize eventList;
@synthesize favorites;

+(DataManager*)sharedDataManager {
    @synchronized(self) {
        if (!singletonDataManager) {
            singletonDataManager = [[DataManager alloc] init];
        }
    }
    return singletonDataManager;
}

- (DataManager*)init {
    if (self = [super init]) {
        eventList = [[NSMutableArray alloc] init];
        favorites = [[NSMutableSet alloc] init];
    }
    return self;
}


#pragma mark -
#pragma mark Data management functions

- (void)addToFavorites:(NSString *)guid
{
    [self.favorites addObject:guid];
    NSLog(@"Item added--we now have %d faves.", [favorites count]);
}

- (void)removeFromFavorites:(NSString *)guid
{
    [favorites removeObject:guid];
    NSLog(!"Item removed--we now have %d faves.", [self.favorites count]);
}


@end

I made my viewcontroller where this is happening call [[DataManager sharedManager] addToFavorites:Event.guid] instead of adding the item right to the favorites set itself, but I left the logging stuff that was there in place.

Here's my log:

2010-05-13 13:25:52.396 EverWondr[8895:207] Toggling on, adding 05db685f65e2
2010-05-13 13:25:52.397 EverWondr[8895:207] Item added--we now have 0 faves.
2010-05-13 13:25:52.398 EverWondr[8895:207] favorites array now contains 0 members
2010-05-13 13:25:53.578 EverWondr[8895:207] Toggling off
2010-05-13 13:25:53.579 EverWondr[8895:207] favorites array now contains 0 members

So.... the DataManager object can't even add anything to its own property! And it doesn't throw an exception like it would if it was a non-mutable type, it just silently fails!

Just for fun, I went through and changed it to an NSMutableArray, which I'm more familiar with. Same behavior.

As phellicks suggest above, you might not be returning a mutable set from data.favorites . Although, you should be getting a compiler warning if that is the case.

This 05db685f65e2 is not a real guid. It looks more like the address of an object. You should check the type on 'thisEvent.guid` to make sure your got an object and the correct type of object.

Unrelated to your main problem, I would add that (1) this:

NSMutableSet *favorites = data.favorites;

... is rather pointless and just adds another possible source of error. There is no reason not to just use data.favorites directly in the code. (see (3) below)

(2) When accessing an external object, even a singleton, it is good practice to make the reference to the external object a property of the class especially in the case of a critical object like a data model. This lets you control and track access to the external object.

(3) Don't treat singletons as naked global variables. This will lead to grief. Instead, wrap access to the data models internal data in specific methods. For example, instead of accessing the data.favorites directly create a method like:

- (void) addToFavoritesGuid:(id) aGuid;

or

- (void) addToFavoritesGuid:(GuidClass *) aGuid;

This will give your data model control over its internals and give it the ability to refuse to add objects that shouldn't belong there.

Edit

From comments:

Okay, re what I'm actually returning... I just used debug to step through my singleton's initializer. Examining the ivars of my DataManager object, I see that my favorites, which is initialized in init with favorites = [[NSMutableSet alloc] init]; is actually getting created as a NSCFSet, and I don't know what that is nor what to make of it..

NSSet like all the collections and strings is actually a class cluster ie a collection of subclasses that all share the same interface. When you create a set the actual class you get back maybe different depending on how it was created. In this case, you're getting back NS-Core-Foundation-Set which is the standard core class for NSSet.

Therefore, your problem is that favorites is initialized as a mutable set but is being assigned to a immutable set. This is why you can't add anything to it.

This initialization:

favorites = [[NSMutableSet alloc] init];

... is being disposed of by:

NSMutableSet *favorites = data.favorites;

If you have an instance variable and you create a local variable of the same name, the local symbol will dominate in the scope it was created in. This appears to work because as a subclass of NSSet, NSMutableSet responds to all the methods and attributes of NSSet.

However, you must be getting a spate of warnings from your linker when you build. You shouldn't ignore those errors. You should treat them as fatal errors because that's what they will be at runtime.

To resolve your problem:

(1) Declare data.favorites as a mutable array and just access it directly. Having another local variable assigned to the same address buys you nothing.

(2) Declare favorites as mutable array property of the current object. Initialize it from data.favorites like: self.favorites=[NSMutableSet setWithCapacity:[data.favorites count]]; [self.favorites setSet:data.favorites]; // ... add or remove items data.favorites = self.favorites;

(3) Move all the logic for adding or removing objects in data.favorites to custom methods in the data model object (see above)

Three is the best choice.

Edit02

It looks like the class clusters are hiding the true classes of all classes in the cluster. I ran the following test code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    NSSet *s=[NSSet setWithObject:@"setWithObject"];
    NSMutableSet *m=[NSMutableSet setWithCapacity:1];
    [m addObject:@"Added String"];
    NSMutableSet *n = [[NSMutableSet alloc] initWithCapacity:1];
    [self showSuperClasses:s];
    [self showSuperClasses:m];
    [self showSuperClasses:n];
    [self showSuperClasses:@"Steve"];   
}

- (void) showSuperClasses:(id) anObject{
    Class cl = [anObject class];
    NSString *classDescription = [cl description];
    while ([cl superclass]) 
    {
        cl = [cl superclass];
        classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    }
    NSLog(@"%@ classes=%@",[anObject class], classDescription);
}

... and got this output:

 NSCFSet classes=NSCFSet:NSMutableSet:NSSet:NSObject
 NSCFSet classes=NSCFSet:NSMutableSet:NSSet:NSObject
 NSCFSet classes=NSCFSet:NSMutableSet:NSSet:NSObject
 NSCFString classes=NSCFString:NSMutableString:NSString:NSObject

Clearly, the report from the debugger and the class function are useless in figuring out the true class of any instance that belongs to cluster. It didn't used to be this way. This is a recent change. I presume its part of the "toll-free bridging" from Core Foundation.

You can add items to favorites because all definitions of favorites in both classes are NSMutableSet.

In any case, your problem is that you have two separate definitions of favorites in the same class. You are getting a warning from the linker saying:

Local declaration of "favorites" hides instance variable

I think the problem can be explained by the runtime confusing the two favorites. You add objects to one favorites but you log the other one.

The local redefinition of favorites serves absolutely no purpose. Remove it and see if the problem persist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM