Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property of mutable type 'NSMutableDictionary' has 'copy' attribute; an immutable object will be stored instead

I am using Xcode9 and I tried to analyse the project.Then I got the following issue like

Property of mutable type 'NSMutableDictionary' has 'copy' attribute; an immutable object will be stored instead

Please go through the image shows the analyze issue

enter image description here

How to resolve this issue?

like image 246
IKKA Avatar asked Aug 31 '25 06:08

IKKA


1 Answers

In Objective-C there is a copy method defined in NSCopying and other is mutableCopy, defined in NSMutableCopying. NSDictionary(superclass of NSMutableDictionary) conforms to both these protocols.

But in property accessors, only copy is available which uses copy method on NSMutableDictionary to create an immutable object, hence the warning when you run the static analyzer, because you are now trying to store an NSDictionary object into an NSMutableDictionary reference.

If you call any NSMutableDictionary method on this reference, I reckon the app should crash.

To solve this issue, you can either use strong that would hold the reference for you. But, if you really need a copy(for whatever reasons), you can write your own setter, something like this:

@property (nonatomic) NSMutableDictionary *parameters;

- (void)setParameters:(NSMutableDictionary *)parameters {
    _parameters = [parameters mutableCopy];
}
like image 146
Puneet Sharma Avatar answered Sep 02 '25 20:09

Puneet Sharma