Search code examples
iosnsmutabledictionary

xcode - correct use of NSMutableDictionary


I defined in a SecondView a public property

@property (weak, nonatomic)  NSMutableDictionary * ToModify;

From firseview, I used that property to passing another dicnary to dsecond view

secondView.ToModify =origin;

But when I try to modify the content of ToModify by setObject method, crash appears:

_NSCFDictionary setObject:forKey:]: mutating method sent to immutable object

It seems that ToModify is a NSDictonary, and in fact by nslog class, it appears as NSDictonary.

Why it occurs?


Solution

  • The compiler complains that origin is an immutable NSDictionary.

    To make it mutable, call mutableCopy

    secondView.ToModify = [origin mutableCopy];