Unrecognized selector when adding an object to NSMutableArray

前端 未结 2 1556
情深已故
情深已故 2020-12-21 21:14

I have a singleton class with a NSMutableArray property to which I want to add objects and remove objects. For some reason I am getting:

-[__NSDictionaryI s         


        
相关标签:
2条回答
  • 2020-12-21 21:24

    Your

    @property (nonatomic, copy) NSMutableDictionary *p_outbox;
    

    is creating a copy of that object which you assign to it. As you are assigning a NSMutableDictionary to it, it's creates a copy of NSMutableDictionary object which is NSDictionary which is not a mutable copy.

    So change it to

    For Non ARC

    @property (nonatomic, retain) NSMutableDictionary *p_outbox;
    

    For ARC

    @property (nonatomic, strong) NSMutableDictionary *p_outbox;
    
    0 讨论(0)
  • 2020-12-21 21:38

    The problem is with your property:

    @property (nonatomic, copy) NSMutableDictionary *p_outbox;
    

    The copy semantics of the property result in a copy of the dictionary being made when you assign a value to the property. But the copy method for a dictionary always returns an immutable NSDictionary, even when called on an NSMutableDictionary.

    To solve this problem you must create your own setter method for the property:

    // I'm a little unclear what the actual name of the method will be.
    // It's unusual to use underscores in property names. CamelCase is the standard.
    - (void)setP_outbox:(NSMutableDictionary *)dictionary {
        p_outbox = [dictionary mutableCopy];
    }
    
    0 讨论(0)
提交回复
热议问题