1

I'm sure this is an complete Noob question... but I've actually never had to deal with this scenario before so I'm a bit befuddled...

Let's say I have a custom object I'll call person, and each person object can have an array of "possessions", a kind of inventory if you will. I would set it up like this:

interface person : NSObject {
    NSString *name;
    NSMutableArray *posessions;

@property (copy) NSString *name;
@property (copy) NSMutableArray *posessions; // no idea if this is even necessary...

}

Of course, I would also synthesize my properties in the implementation file... Now, in my actual controller object, I would make an instance of my object (or usually an array of instances, but for this example, one will work fine...) as so:

person *aPerson;

I know that to access the persons name, I could make a call like this:

[aPerson setName:@"Bob"];

and to retrieve that name, I might use this:

aVar = [aPerson name];

What I'm stuck on is how exactly would I go about adding or retrieving objects to the NSMutableArray located inside my person class? Let's say I want to use the "count" method for the NSMutable Array.

I've done some trial and error with attempts such as:

[aPerson.posessions count];
[[aPerson posessions] count];

Likewise, to add an object to an array, I have often used:

[someArray addObject:anObject];

but attempts like this haven't worked:

[aPerson.posessions addObject:anObject];

After reading up a bunch and searching the web, I can't seem to find exactly how to interact with this NSMutableArray in my custom class. I'm sure it's something obvious that I'm just not quite getting, and it's become a sort of mental block...

Also, am I correct in synthesizing accessor properties for the NSMutableArray? If so, setX and X don't seem to be quite so obvious with NSMutableArray... unless they simply copy the entire array into a local variable...

Perhaps is this what needs to be done? use the accessor methods to get the entire array, place it in a local variable, make my changes, then use the set accessor method to put the entire array back into my person object?

Can someone enlighten me a bit on the syntax I should be using here?

* EDIT *

I thought I'd add a bit of clarification to this question. My custom objects (in the above example, my person object) are basically database records. I have several databases I am working with in my project, so for example:

Person - a custom sub-class of NSObject containing multiple NSString Objects, as well as Ints and BOOLs.

personDatabase - An Array of Person objects (set up and controlled within my main CONTROLLER object)

All of the set and get methods are called from "Controller".

What I have been attempting to do is to directly access the individual objects contained within the personDatabase from within my Controller object. I have done this by declaring another object this way:

Person *activePerson;

Then, all of my calls are made to the currently active Person record (the one currently selected from the personDatabase), such as:

someOutput = [activePerson name];

etc.

Is there a way to directly access the objects inside the NSMutableArray object inside the activePerson object from my Controller object?

2
  • When you say "Attempts like this haven't worked" do you mean it hasn't added it to the array, or it gets an error? Commented Jun 1, 2012 at 20:35
  • the array always returns a count of 0 regardless of which way I've attempted to manipulate it, so It's either not adding an object at all, or it's doing something completely unexpected (to me). I'm mainly trying to understand the proper syntax to use to add an object or read an object from the array. Commented Jun 1, 2012 at 23:00

1 Answer 1

2

You've specified the 'possessions' property as 'copy'. Therefore, when you write aPerson.possessions you are getting a copy of the possessions array. The call to addObject adds anObject to a new array that is a copy of aPerson's array of possessions. The simplest 'fix' would be to change 'copy' to 'retain' (and probably 'readonly'). [Edit: Wrong; it is 'copy on assign' - not 'copy on read']

However, there is a bigger issues. A person has possessions but how you store them is an implementation detail. When you put NSMutableArray in the public interface you overly restrict your implementation. You might be better served to change the Person interface along the lines of:

@interface Person : NSObject {
@private
     NSString *name;
     // ...
  }
  - (Boolean) addPossession: (NSObject *) obj;
  - (Boolean) remPossession: (NSObject *) obj;
  - (Boolean) hasPossession: (NSObject *) obj;
  - (NSArray *) allPossessions;
@end

Then, how you implement these possession methods depends on if you use an array, a set, a linked-list, a tree, a whatever.

Sign up to request clarification or add additional context in comments.

9 Comments

I would expect then that the 'copy' of the array should return a count of 1 then... in the example above, the count method returns 0 which I would think means that no object is being added to the array. In this particular case, it's not so much that it's not adding to the correct instance of the array, it's more than it's not adding an object to ANY instance of the array. I'm thinking it's my syntax that is the problem.
Ah. Hadn't thought about doing it that way. Certainly a clean "object oriented" approach. I'm pretty sure that would solve my problem. This is literally the only array contained within any of my custom classes for this project. Every other iVar is a non-array, so I've been able to use the accessor methods for everything up to this point.
Everytime you call aPerson.possessions you get another new copy (which will have 0 objects in it). [aPerson.possessions addObject: obj]; NSAssert (0 == [aPerson.possessions count]);
Ah.. I think part of the problem is that I have never worked with the object.ivar setup. I always use method calls with accessor methods [object ivar]... If I'm understanding correctly, they behave differently? all of my NSString objects inside the person object are set to (copy) as well, and I was expecting the NSMutableArray to behave the same way as the NSString objects within Person...
Actually the string and array do behave similarly, they are both copied when accessed. But, you are using them differently - you are trying to modify the array. If you tried to modify the string you'd have the same problem whereby the change wouldn't stick.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.