Re: Newbie-setting object used as variable of another object
Re: Newbie-setting object used as variable of another object
- Subject: Re: Newbie-setting object used as variable of another object
- From: Joshua Scott Emmons <email@hidden>
- Date: Tue, 24 Jan 2006 11:40:13 -0600
Because I'm just a "hobby programmer" I often run across concepts I
just don't grasp.
Been there. Don't worry, it gets better with time.
So, can someone help me out on this one. I have an object (which
has variables of it's own) as a variable of another object but I
can't seem to set values to the object that is a variable.
One things that is going to help along your understanding (and help
you get more responses when posting to a mailing list) is
understanding the proper jargon associated with a language. It will
really pay off if you study the Objective-C glossary at http://
tinyurl.com/77few and use that language in your emails.
For instance, I think what you are saying above is: I have an ObjectA
that has, as one of its instance variables (A.K.A. ivar), an instance
of ObjectB. I can't seem to set the instance of ObjectB to set any of
ObjectB's ivars. What's going on?
[peopleArray addObject: aPerson = [[People alloc] init]];
[aPerson setValue:[tmpArray objectAtIndex:0] forKey:@"personID"];
[aPerson setValue:[tmpArray objectAtIndex:1] forKey:@"personName"];
To make things easier on yourself, I suggest you follow the practice
of not naming things in plural unless they are collections (arrays,
sets, etc). It looks as though your People class is not an array. It
should therefore be called "Person". Then, if you make an array to
hold a bunch of Person objects, you can call the array "people". The
same below with your Things class. If it's not an array, call it
"Thing". It will make things easier to understand. Trust me.
[aPerson setValue:aThing forKey:@"thing"];
I get a message in the console that says...
-[Things copyWithZone:]: selector not recognized [self = 0x30ed10]
In Objective-C, a selector is the name of a method (more or less.
Again, look at the glossary for more). What this error is telling you
is that your instance of Things (in this case, "aThing") has been
asked to respond to a method called "-copyWithZone:", and it doesn't
know how (the selector is not recognized by Things).
The question you must answer to get your code working is two-fold:
1) Why is aThing being sent -copyWithZone:?
2) Why doesn't aThing know how to respond to -copyWithZone:?
The answer to number one is a little tricky because we don't know
anything about the implementation of your objects. But generally, -
copyWithZone: gets called because you tried to make a copy of the
instance of your class:
http://tinyurl.com/327ad
Does -[People setThing:(Things *)aThing] contain "[aThing copy]"
anywhere in it?
The answer to the second question is: Unless Things implements the
NSCopying protocol (http://tinyurl.com/aeblj), or inherit from
something else that implements NSCopying, it doesn't get -
copyWithZone: for free. You have to implement it your self (or
override -copy:, which is the thing calling -copyeWithZone:).
I'm guessing that your Things class is a subclass of NSObject. And as
you can see in NSObject's documentation for -copy: (http://
tinyurl.com/327ad), NSObject does not, itself, implement the
NSCopying protocol, leaving that up to subclasses.
Implementing NSCopying in your Things class would be a tricky thing
for a hobbyist programmer to pull off. A better solution is to not
copy it (and just retain it instead, if your desired functionality
allows this), or to find a better class to subclass from -- one that
already implements NSCopying.
Actually, from the small snipets of code that you provide, it looks
like the only thing Things does is hold a number. I'd suggest
replacing Things all together with NSNumber (http://tinyurl.com/237tp).
Hope that's enough to get you on the right track.
-Joshua Emmons
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden