Reputation: 869
In my project I am having one object of type id. I am setting different values to it. Now I need to check which value it contains and according to that I need to do the operation.
This is my code:
id *currentObject=nil;
-(void)setCurrentObject:(id *)object{
currentObject=object;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:@"apple"]) {
[self setCurrentObject:apple];
return;
}
else if ( [elementName isEqualToString:@"orange"]) {
[self setCurrentObject:orange];
return;
}
[self readString];
}
-(void) readString{
//Here I need to check contents of 'currentObject'. If it contains 'apple' then print 'Its apple' else don't do anything
}
How to check this content of current object which is of type 'id'? Please help.
Upvotes: 0
Views: 71
Reputation: 8106
you can get the class name of your object by using
NSStringFromClass
you might want to determine what kind of class your ID object is
id object;
NSDictionary *str = [NSString stringWithString:@"test string"];
object = str;
NSString *string = NSStringFromClass([object class]);
NSLog(@"object is class of : %@", string);
if ([object isKindOfClass:[NSString class]]) {
NSLog(@"data string = %@",(NSString*)object);
}else if([object isKindOfClass:[NSDictionary class]]){
NSLog(@"data dictionary = %@", (NSDictionary*)object);
}
Upvotes: 1
Reputation: 25917
If you know what kind of object you are going to store ("apple" or "orange"), why don't you just create a NSString
instead of id
? Don't forget the KISS principle.
Upvotes: 0
Reputation: 3908
To get object's class name you can use
const char* className = class_getName([yourObject class]);
Then create a nsstring from above classname using utf8 string then check against what do you want.
Upvotes: 0