Mutable @dynamic properties

22 views
Skip to first unread message

Brendan Duddridge

unread,
Jul 17, 2015, 4:17:08 PM7/17/15
to mobile-c...@googlegroups.com
Hi,

I have a property on my class called "values". It's an NSMutableDictionary.

However, I guess the JSON parser doesn't like that it's mutable. Is there a way to tell CBL on iOS that I want my values property to be mutable?

I get the following error otherwise:

WARNING: Dynamic property TFFormEntry.values has type '@"NSMutableDictionary"' unsupported by TFFormEntry


Thanks,

Brendan 

Jens Alfke

unread,
Jul 17, 2015, 5:36:15 PM7/17/15
to mobile-c...@googlegroups.com

On Jul 17, 2015, at 1:17 PM, Brendan Duddridge <bren...@gmail.com> wrote:

I have a property on my class called "values". It's an NSMutableDictionary.

However, I guess the JSON parser doesn't like that it's mutable. Is there a way to tell CBL on iOS that I want my values property to be mutable?

CBLModel doesn’t support mutable types. The problem is detecting changes — if you modify the contents of the dictionary, the dictionary property itself isn’t changed, so the model object doesn’t know it needs to save back to the database. (I believe Core Data’s NSManagedObject has the same limitation.)

In general, mutable property objects are considered a bad idea by many people, because the owning object loses control over the property value, which can be mutated at any moment. (There’s been discussion of this on cocoa-dev recently.)

—Jens

Brendan Duddridge

unread,
Jul 17, 2015, 7:15:30 PM7/17/15
to mobile-c...@googlegroups.com
So to work around it, I'm creating a mutable copy of the values dictionary, then setting my individual properties in the values dictionary, then setting the entire dictionary back using self.values = [NSDictionary dictionaryWithDictionary:mutableDict];

Does that seem reasonable?

I'd love to read the discussion on cocoa-dev if you have a link to that.

Thanks!

Brendan

Jens Alfke

unread,
Jul 17, 2015, 7:44:43 PM7/17/15
to mobile-c...@googlegroups.com

On Jul 17, 2015, at 4:15 PM, Brendan Duddridge <bren...@gmail.com> wrote:

So to work around it, I'm creating a mutable copy of the values dictionary, then setting my individual properties in the values dictionary, then setting the entire dictionary back using self.values = [NSDictionary dictionaryWithDictionary:mutableDict];

That’s fine, although self.values = [mutableDict copy] is shorter.

—Jens
Reply all
Reply to author
Forward
0 new messages