What's safe to assume about the NSMutableArray / NSArray class cluster?
Posted
by andyvn22
on Stack Overflow
See other posts from Stack Overflow
or by andyvn22
Published on 2010-04-05T19:04:38Z
Indexed on
2010/04/05
19:23 UTC
Read the original article
Hit count: 180
I know you shouldn't use this to decide whether or not to change an array:
if ([possiblyMutable isKindOfClass:[NSMutableArray class]])
But say I'm writing a method and need to return either an NSMutableArray
or an NSArray
, depending on the mutability of possiblyMutable
. The class using my method already knows whether or not it's acceptable to change the returned array. Whether or not it's acceptable to change the returned array directly correlates with whether or not it's acceptable to change possiblyMutable
.
In that specific case, is this code safe? It seems to me that if it's not acceptable to change the array, but we accidentally get a mutable array, it's ok, because the class using my method won't try to change it. And if it is acceptable to change the array, then we will always get possiblyMutable
as an NSMutableArray
(though this is the part I'm not entirely clear on).
So... safe or not? Alternatives?
© Stack Overflow or respective owner