Re: objc_getClassList, pointers, and NSArray
Re: objc_getClassList, pointers, and NSArray
- Subject: Re: objc_getClassList, pointers, and NSArray
- From: Chris Kane <email@hidden>
- Date: Mon, 9 Jul 2001 09:59:31 -0700
On Monday, July 9, 2001, at 02:04 AM, David Remahl wrote:
I've used the sample code in /usr/include/objc/objc_runtime.h to get a
list of classes. The sample code is as follows:
[...]
Problem is, I want to put the classes into an NSArray (or subclass
thereof)
for use elsewhere in my application. Unfortunately, I missed a great
deal
of the stuff about pointers in my C class due to illness, so I'm not
entirely clear on procedure here.
Hmm, you can use the ususal C array stuff; myArray[elementToGet].
So,
NSMutableArray arrayWithStrings = [[NSMutableArray alloc] init];
for( i=0;i<numClasses;i++)
{
[arrayWithStrings addObject:[NSStringFromClass(classes[i]) retain]];
}
Careful here. There's a "leak" at least (over-retain). The -retain
message is unnecessary since the NSArray will do that, and you're likely
to forget elsewhere to send -release to every contained object in that
array before releasing the array.
The ObjC classes are objects, as well, so you can put them directly in
the NSArray.
NSMutableArray classes = [[NSMutableArray alloc] init];
for (i = 0; i < numClasses; i++) {
[classes addObject:classes[i]];
}
Using the string name of the class, though, is a legitimate way to go
about saving this information, and then you use NSClassFromString()
later to "convert" back to the class object.
There are a few subtle situations in which you will actually get
different results over time (during execution of the program) depending
on which technique you use. If somebody pipes up wanting to know more
about this, perhaps some of the advanced developers on the list will
explain further.
Chris Kane
Cocoa Frameworks, Apple