Object Arrays in Objective-C

Member
Posts: 215
Joined: 2008.06
Post: #1
I've been using NSArray to pass around collections of objects for quite a while now, but I'm sick of not having the inherent type checking. Is there a way to create an array of objects without using NSArray? Such as...

MyClass* objects[];

- (id) initWithObjects:(MyClass*[])passedObjects
{
objects = passedObjects;
}

I've tried this and gives me "incompatible types". Is there a way to create and pass object arrays? Thank you.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
What you're trying to do is certainly possible, but you can't assign whole arrays like that. Something like this would work:

Code:
MyClass ** objects;

- (id) initWithObjects: (MyClass **) passedObjects count: (int) count {
  objects = malloc(sizeof(MyClass *) * count);
  memcpy(objects, passedObjects, sizeof(MyClass *) * count);
}
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #3
ThemsAllTook Wrote:What you're trying to do is certainly possible, but you can't assign whole arrays like that. Something like this would work:

Code:
MyClass ** objects;

- (id) initWithObjects: (MyClass **) passedObjects count: (int) count {
  objects = malloc(sizeof(MyClass *) * count);
  memcpy(objects, passedObjects, sizeof(MyClass *) * count);
}

Aww! Thanks for the info. That kind of feels like a hack... That sucks I can't do it any other way. Oh, well. Thanks ThemsAllTook.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
If you're worried about static typechecking you're not ObjC'ing properly Wink

Forget your Java-, C++- and C#-inspired hang-ups, and embrace the dynamicity.

Honestly, if you don't know what kind of object is in an array, you've got bigger problems than a lack of static typing Rasp
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #5
OneSadCookie Wrote:Honestly, if you don't know what kind of object is in an array, you've got bigger problems than a lack of static typing Rasp

Wait you guys just dont toss random objects in arrays? I think im doing it wrong Wacko

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #6
If you really want some static type checking, you could create a wrapper class with a single NSArray member and do your own type checking before passing the method call to NSArray. However, as OSC said, if you have to have the static type checking you might want to re-think how you add objects to your array.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #7
Blacktiger Wrote:If you really want some static type checking, you could create a wrapper class with a single NSArray member and do your own type checking before passing the method call to NSArray. However, as OSC said, if you have to have the static type checking you might want to re-think how you add objects to your array.

That's pretty much what I'm doing right now, but since I'm making this to be as generic as possible, some extra type checking wouldn't hurt whatsoever. I don't think that static type checking is ever a bad idea and I was just wondering if there was a better way to leverage the runtime environment. Thanks for the info.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
⌘-R in Chief
Posts: 1,256
Joined: 2002.05
Post: #8
But it's.... unnecessary.....

The whole stinkin' operating system runs on this methodology. There's no reason to fight it.

2¢
Quote this message in a reply
Post Reply