Questions tagged [nsmutablecopying]

The NSMutableCopying protocol declares a method for providing mutable copies of an object. Only classes that define an “immutable vs. mutable” distinction should adopt this protocol. Classes that don’t define such a distinction should adopt NSCopying instead.It is available in iOS 2.0 and later.

This is declared in Apple's Foundation framework. A class that defines an immutable vs. mutable distinction adopts this protocol to allow mutable copies of its instances to be made. A mutable copy of an object is usually a shallow copy (as opposed to the deep copy defined in the NSCopying protocol specification). The original and its copy share references to the same instance variables, so that if a component of the copy is changed, for example, that change is reflected in the original.

From Apple's official document:

NSMutableCopying declares one method, mutableCopyWithZone:, but mutable copying is commonly invoked with the convenience method mutableCopy. The mutableCopy method is defined for all NSObjects and simply invokes mutableCopyWithZone: with the default zone.

If a subclass inherits NSMutableCopying from its superclass and declares additional instance variables, the subclass has to override mutableCopyWithZone: to properly handle its own instance variables, invoking the superclass’s implementation first.

Source: NSMutableCopying Protocol Reference

Related SO question:

  1. What does Mutable Copy in Objective C do

  2. MutableCopy AllocLeak MemoryLeak

Related tags:

11 questions
7
votes
1 answer

How to implement properly mutableCopyWithZone and copyWithZone

I read others few topics about it, but still I'm lost. I want to create 2 kind of objects, one immutable with only "readonly" properties, and one mutable with only "readwrite" properties. Lets call them EXCar and EXMutableCar. EXCar is subclass of…
Tancrede Chazallet
  • 7,035
  • 6
  • 41
  • 62
3
votes
2 answers

Objective-C pattern for creating mutable copies

I have many "model" objects whose properties are defined as "readonly" and shared among various components. In some cases I need to create local mutable copies of the objects (using them for local mutable state) I rather not implement NSMutableCopy…
Avba
  • 14,822
  • 20
  • 92
  • 192
2
votes
0 answers

Is this a reasonable pattern for implementing NSMutableCopying?

I'm looking to implement the NSMutableCopying interface as I have a set of objects which are immutable, but I also need to be able to create modified copies. Let's assume we've got a movie defined something like this (of course, really there would…
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
1
vote
2 answers

What does Mutable Copy in Objective C do

In objective C, same to C++ language, if you assign object to object it will get its pointing address like this: object1 = object2; So changing in one of objects above will affect the other one. Is this the same for MutableCopy? and what is the…
Mohanad Kaleia
  • 793
  • 1
  • 9
  • 22
1
vote
1 answer

Using -mutableCopyWithZone: on custom class makes it immutable

I've created a custom class which conforms to NSCopying and NSMutableCopying. I've added an implementation for -copyWithZone: and -mutableCopyWithZone:, but whenever I call -mutableCopy on my object and try to call another method, it crashes because…
Jack Greenhill
  • 10,240
  • 12
  • 38
  • 70
0
votes
1 answer

Why does this NSMutableAttributedString addAttribute work only if I use a mutableCopy

I have the following code : NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"]; NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"]; [boldS…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
0
votes
1 answer

NSDictionary: Comparison after MutableCopy

I have the following properties: @property (retain, nonatomic) NSMutableDictionary * firstStartTimeObject; @property (retain, nonatomic) NSMutableDictionary * firstLocationNameObject; @property (retain, nonatomic) NSMutableDictionary *…
user1107173
  • 10,334
  • 16
  • 72
  • 117
0
votes
1 answer

Issue related with modifying the value in an NSMutablearray

I am facing an issue with the values in NSMutablearray. I have two NSMutablearray, both are holding the same content using mutablecopy. The issue is that when I modify a value in one array, the corresponding value in the second array also get…
Abz Ios
  • 64
  • 7
0
votes
1 answer

mutableCopyWithZone confusing requirement?

Clarification: the question is not about mutable vs immutable but about a call to super creating an instance of the correct class. I hadn't thought of using [[self class] alloc] in the base class, and this seems to solve my issues. I'll accept that…
verec
  • 5,224
  • 5
  • 33
  • 40
0
votes
4 answers

MutableCopy AllocLeak MemoryLeak

I have an NSTimer that fires once per second. And every second I have an NSString that needs to be changed. I've never tried to deal with memory management before so I'm not sure if what I'm doing is right but instruments is saying under "alloc"…
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
0
votes
2 answers

Literal syntax vs. mutableCopy and Objective-C compiler warnings

I've more experience with Python than anything else, so to work with mutable vs. immutable arrays and dicts is a little weird. I'm really enjoying the literal syntax that Xcode 4.5 has added, but in doing some simple messing about I come across a…
user890167