More about the platform than I wanted to know
2024-06-03 19:41:40.181065+02 by
Dan Lyke
5 comments
So, in Objective-C, if you do:
self.itemsDataNew = [@[] copy];
self.itemsDataFeed = [@[] copy];
Do they point to the same empty array object, or different ones?
Damn it, Apple...
[ related topics:
Apple Computer Interactive Drama
]
comments in ascending chronological order (reverse):
#Comment Re: More about the platform than I wanted to know made: 2024-06-03 19:51:46.119226+02 by:
markd
I'd say they point to the same empty array object. The empty set is the empty set, and since it's an
immutable copy, it can't be changed, so why pay the price of another malloc. In general, I believe a copy to
an immutable array (not a mutable one hidden behind an immutable type) returns `self` as an optimization.
Identity checks on Foundation objects are always slippery, given the class cluster(fuck) architecture of the
fundamental collection and string types.
From a test, it looks like `copy` has a common empty array that it uses. doing [@[] mutableCopy] will give
you a different address, but if you(immutable) copy that
, you'll get the same address as @[] and [@[]
copy]. I imagine the `copy` implementation starts with if (source.count == 0) return
SharedSingletonCommonEmptyArray;
#Comment Re: More about the platform than I wanted to know made: 2024-06-04 01:56:08.718655+02 by:
Dan Lyke
[edit history]
And you've clearly done more Objective-C than anyone else who responded on Mastodon.... Moved to mutable arrays and
everything works like I'd expect (because I'm using these arrays in an NSOutlineView and using their relationship to those
member pointers for additional information about them).
#Comment Re: More about the platform than I wanted to know made: 2024-06-04 01:56:30.499723+02 by:
Dan Lyke
[edit history]
Whoops. Double post.
#Comment Re: More about the platform than I wanted to know made: 2024-06-04 02:03:47.936171+02 by:
TheSHAD0W
I mean, that's something testable. If you insert something into itemsDataNew and it shows up in itemsDataFeed you know you have an issue. Of course, you'll have to hope a code change doesn't screw that up.
#Comment Re: More about the platform than I wanted to know made: 2024-06-04 16:57:17.157457+02 by:
Dan Lyke
It's made a little harder by the fact that Objective-C managed arrays are by default read-only. So, yeah, the
solution was to make them mutable arrays.