Problem with properties and setters; also:Surprising behavior of NSMutableArray - makes spontaneous copy
Problem with properties and setters; also:Surprising behavior of NSMutableArray - makes spontaneous copy
- Subject: Problem with properties and setters; also:Surprising behavior of NSMutableArray - makes spontaneous copy
- From: David Hirsch <email@hidden>
- Date: Mon, 14 Dec 2009 15:01:51 -0800
(After drafting the first version of this email, I found a sort-of
solution, but I do not understand why it works. I thought that simple
properties (e.g., object.foo) essentially just called the foo:/setFoo:
methods, particularly if you used @property (assign). That is
apparently not the case. See the code for a working and non-working
version. Note that I do not have custom setters/getters; I've
synthesized them.)
I have a Configuration object that has an NSMutableArray of instances
(i.e., the class name is "Instance" - this is not the computer science
definition of "instance"). Each Instance has a course, room,
instructor (which are, respectively, Course *, Room *, Instructor *).
In the newNearbyConfiguration method of Configuration, I need to make
a slightly modified copy of the Configuration object. To do this, I
choose an Instance from the array, modify it (for example, by changing
the room to a different Room *). Oddly, however, when I make this
change, the array suddenly gets a new copy of the original Instance.
The new copy has a new memory address. I would have expected that I
can modify the contents of an object in an NSMutableArray without
needing to replace the original.
The Instance * should still point to the same memory, right?
Also, I put a breakpoint in Instance copyWithZone, but that was
apparently not called.
Can anybody shed light on this? Here is some relevant code:
(From Instance.h:)
@interface Instance : NSObject {
Course * course; // shallow copy
WeekPattern * pattern; // shallow copy
Instructor * instructor; // Shallow copy
Timing * timing; // Deep copy
Room * room; // Shallow copy
NSUInteger quarter; // Simple copy 0=fall, 1=winter, 2=spring
}
@property (assign) Course * course; // weak reference
@property (assign) WeekPattern * pattern; // weak reference
@property (assign) Instructor * instructor; // weak reference
@property (assign) Timing * timing; // strong reference - I own this
@property (assign) Room * room; // weak reference
@property (assign) NSUInteger quarter;
(From Configuration.h:)
@interface Configuration : NSObject <NSCopying> {
NSMutableArray *instances; // array of Instances
ClassSchedulerDoc *myDoc; // weak reference
}
@property (retain) NSMutableArray *instances;
@property (assign) ClassSchedulerDoc *myDoc;
(From Configuration.m, highly edited for brevity)
- (Configuration *) newNearbyConfiguration {
// make a new configuration like the current one but with one small
change
Configuration *newConfig = [self copy];
short randInstance = rand() % [self.instances count];
Instance *instanceToAlter = [self.instances
objectAtIndex:randInstance];
Room *newRoom;
do {
newRoom = [[instanceToAlter course] randomLowCostRoom];
} while (newRoom == [instanceToAlter room]);
[instanceToAlter setRoom: newRoom]; // this works
// instanceToAlter.room = newRoom; // this displays the problem
described above
return newConfig;
}
- (NSString *)description {
NSMutableString *desc = [NSMutableString stringWithCapacity:200];
for (Instance *thisInstance in instances) {
[desc appendString:[thisInstance briefDescription]];
}
return desc;
}
Also, I do know that I could easily have just made Configuration
inherit from NSMutableArray. That would appear to be irrelevant to my
question.
Thanks,
Dave
============================
Dave Hirsch
Associate Professor
Department of Geology
Western Washington University
persistent email: email@hidden
http://www.davehirsch.com
voice: (360) 389-3583
aim: email@hidden
vCard: http://almandine.geol.wwu.edu/~dave/personal/DaveHirsch.vcf
============================
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden