1

Possible Duplicate:
What's the best way to use Obj-C 2.0 Properties with mutable objects, such as NSMutableArray?

Please excuse my poor English :)

In Objective C, the 'copy' qualifier doesn't retain the mutability. We have to make a property 'strong' or provide a setter ourselves.

I just want to know why the 'mutablecopy' qualifier is not provided. Any specific reason or design tradeoffs ?

Community
  • 1
  • 1
fioman
  • 33
  • 3
  • 3
    related question: http://stackoverflow.com/questions/816720/whats-the-best-way-to-use-obj-c-2-0-properties-with-mutable-objects-such-as-ns – Felix Aug 01 '12 at 15:23
  • why don't use the `[NSMutableArray arrayWithArray:...];` or `[NSMutableDictionary dictionaryWithDictionary:...];` init methods instead of thinking of the _mutableCopy_? :) – holex Aug 01 '12 at 15:24
  • @holex I'm not sure how that helps the OP's question. Anyway is there any technical reason to prefer the `init` variants over `mutableCopy`? I'd think the `mutableCopy` is less typing and I wouldn't be surprised if they was implemented in terms of each other. – Paul.s Aug 01 '12 at 15:36
  • @phix23 yes,i know how 2 make a property mutable. I just wanna know why the language leave the job to programmers. why not just provide a qualifier such as 'mutablecopy'? the tradeoffs that effect the final design of language. – fioman Aug 01 '12 at 15:37
  • @fioman to summarise the link above - it's not a common thing to do and there are mechanisms (KVC) that can be used and allow you to hide your implementation details. – Paul.s Aug 01 '12 at 15:40
  • @Paul.s you are absolutely right, but we can solve in another way what _mutableCopy_ would solve. so this is not really missing but, fact, it would be a convenience keyword. – holex Aug 01 '12 at 15:41
  • @Paul.s mutableCopy gives you an object that you own. It doesn't matter so much with ARC but in olden days, the less typing was offset by having to release or autorelease the returned object. – JeremyP Aug 01 '12 at 15:57

1 Answers1

3

Why no 'mutablecopy'?

Because making a mutable copy actually has deep and subtle meaning that makes it nontrivial. As well, you pretty much never want an objects internal storage to be externally mutable.

Also, if properties were to support mutable copy, there would need to be a version for the setter and one for the getter as each has a very different potential role (if you really did want a mutable property, you might want a mutable copying setter and normal retain getter. Or the other way around.).

None of this addresses the even nastier issue of deep versus shallow mutable copy.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • thanks a lot. i messed up with 'initializing' & 'modifying'.i tried to initialize the data by copying and then modify it internally. i should use strong property & initialize it with the empty & always modify it. 'copy' is a init qualifier. – fioman Aug 01 '12 at 16:04