Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-02-2009, 05:23 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: UK
Posts: 118
CarlosT is on a distinguished road
Default instance class

Hi
not sure is this is more of a game development thing, but I would like to know if it's possible to return the class of an object instance and then see if it matches a list of possible types I am looking for.
eg.
Code:
classType = theClassOfObject(myInstance); //this is not right but its kinda what I am wanting to do?!
if (classType  == "classType_1") {
//...stuff to do it its that class
} else... //etc
I would have thought this would be possible but I just don't know what to look for. I'm sure i saw something about this somewhere else when I was looking for something else but now I can't find it! Typical! Does anyone have any *pointers

Thanks!
__________________
My App site http://thetappyappco.com/
CarlosT is offline   Reply With Quote
Old 04-02-2009, 05:33 AM   #2 (permalink)
ob1
Registered Member
 
ob1's Avatar
 
Join Date: Mar 2009
Location: Switzerland
Age: 35
Posts: 97
ob1 is on a distinguished road
Default

from memory you can probably use something like

if ([yourobject isa] == NSClassFromString(@"yourclassname") {};

Can't remember the exact syntax though.
ob1 is offline   Reply With Quote
Old 04-02-2009, 05:58 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: UK
Posts: 118
CarlosT is on a distinguished road
Default

Quote:
Originally Posted by ob1 View Post
from memory you can probably use something like

if ([yourobject isa] == NSClassFromString(@"yourclassname") {};

Can't remember the exact syntax though.
Looks promising, I will have a look and see if that works
__________________
My App site http://thetappyappco.com/
CarlosT is offline   Reply With Quote
Old 04-02-2009, 06:22 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: UK
Posts: 118
CarlosT is on a distinguished road
Default

With your help I came across this code,
Code:
BOOL isClass = ![object1 isMemberOfClass:[object1 class]];
and adapted it to:
Code:
if (myInstance isMemberOfClass:[theClassIAmCheckingFor class]] == YES ) {
//stuff to do if it is
} else {
//stuff to do if it is NOT
}
Is this a good way of checking an instance is a member of a class?
__________________
My App site http://thetappyappco.com/
CarlosT is offline   Reply With Quote
Old 04-02-2009, 06:38 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 249
gabacus is on a distinguished road
Default

yes, there is no problem doing it this way. i think you will have to be careful with inheritance though...

for example, if you had a bunch of NSString's and NSMutableString's and you wanted to check for NSString's only, it will return YES always because NSMutableString is a subclass of NSString... hope that makes sense...

do a google search in class introspection with objective-c, there should be a lot of information out there. i think there are a few isMemberOf... isKindOf methods available to use and they each behave a little differently
gabacus is offline   Reply With Quote
Old 04-02-2009, 07:17 AM   #6 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: UK
Posts: 118
CarlosT is on a distinguished road
Default

Quote:
Originally Posted by gabacus View Post
yes, there is no problem doing it this way. i think you will have to be careful with inheritance though...

for example, if you had a bunch of NSString's and NSMutableString's and you wanted to check for NSString's only, it will return YES always because NSMutableString is a subclass of NSString... hope that makes sense...

do a google search in class introspection with objective-c, there should be a lot of information out there. i think there are a few isMemberOf... isKindOf methods available to use and they each behave a little differently
In my case I have a GameObject class and then a few differernt types of enemy_type_1, enemy_type_2, enemy_type_n sub classes. I am only checking for the different types of the enemies. I think it should be able to differentiate betweeen them (fingers crossed) I will have a look a them the ones you suggested, they sound more powerful/detailed.

Cheers
__________________
My App site http://thetappyappco.com/
CarlosT is offline   Reply With Quote
Reply

Bookmarks

Tags
class, match, object

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 326
6 members and 320 guests
blueorb, guusleijsten, Kryckter, LEARN2MAKE, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0