How to check if an object is a Core Foundation Object in Cocoa?

Neither of those is true. Only some classes conform to NSCoding not all of which are in Foundation, and only a select few classes are property-list classes.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm talking about NSString, NSArray, NSDictionary, etc. EDIT: First of all I meant Foundation object, not Core Foundation object. Let me elaborate a bit more... Foundation objects (AFAIK) have some shared characteristics: They all implement the NSCoding protocol, they can all go into a PList, etc. So for instance if I wanted to archive an object graph to disk, it would be useful to make sure that any objects I add to the object graph are either Foundation objects or my custom objects which I've implemented NSCoding on and it seems silly to do: if (myObject isKindOfClass:NSString class || myObject isKindOfClass:NSNumber class || myObject isKindOfClass:NSArray class || myObject isKindOfClass:NSDictionary class || myObject isKindOfClass:NSSet class || ...) { //add myObject to object graph } That's a simple but maybe useless example, the problem I'm having is in some code I've written that converts a dictionary into a url params string using a format string with the %@ specifier, but I don't wan't arbitrary objects to go in there because I don't want memory addresses in my url params: //convert dictionary into url params string postDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { if (key isKindOfClass:NSString class && (obj isKindOfClass:NSString class || obj isKindOfClass:NSNumber class)) { tempPost appendFormat:@"%@=%@&", key, obj; } else { *stop = TRUE; } }; NSString *post; if (tempPost length > 0) { post = tempPost substringToIndex:tempPost length - 1; } objective-c cocoa core-foundation link|improve this question edited Dec 13 '11 at 16:13 asked Dec 13 '11 at 14:59lms874215 84% accept rate.

Every NSString is both a Foundation and a Core Foundation (CFStringRef) object. They are the same. That's why you see NSCFString as the underlying type of a string in the debugger, for example.

– Ole Begemann Dec 13 '11 at 15:02 Please see my edits – lms Dec 13 '11 at 16:13.

Foundation objects (AFAIK) have some shared characteristics: They all implement the NSCoding protocol, they can all go into a PList, etc. Neither of those is true. Only some classes conform to NSCoding, not all of which are in Foundation, and only a select few classes are property-list classes. You can certainly generate a plist from an object using one of Apple's archivers, but then the object must conform to NSCoding—see above.

So for instance if I wanted to archive an object graph to disk, it would be useful to make sure that any objects I add to the object graph are either Foundation objects or my custom objects which I've implemented NSCoding on … You can test whether an object conforms to NSCoding with someObject conformsToProtocol:@protocol(NSCoding). If you need to test whether an object is a property-list object, you'll have to do that by testing class membership, like you showed in your question. Such a test will include Core Foundation property-list objects, and that is correct: CF property-list objects are just as valid in a property list as their Foundation counterparts, largely because CF and Cocoa property-list objects are interchangeable (due to the toll-free bridge).

The full list of valid property list classes is in both the Property List Programming Guide and the Property List Programming Topics for Core Foundation. The class names are different, of course (CFString vs. NSString), but because of toll-free bridging, they are interchangeable. For all intents and purposes, an NSString is a CFString and vice versa, and the same for every other pair of toll-free-bridged classes.

It isn't helpful to think of “NSStrings vs. CFStrings”; instead, remember that they are the same thing, and treat them as such. The problem I'm having is in some code I've written that converts a dictionary into a url params string using a format string with the %@ specifier, but I don't wan't arbitrary objects to go in there because I don't want memory addresses in my url params… That's a valid case for class-membership testing. //convert dictionary into url params string if (key isKindOfClass:NSString class && (obj isKindOfClass:NSString class || obj isKindOfClass:NSNumber class)) { tempPost appendFormat:@"%@=%@&", key, obj; } You will find an & at the end of your URL that way.

I previously described a specification for an object that formats URL query strings. You can include your class-membership tests within your implementation of that object.

Thanks for the detailed answer! I have one question, what is the collective name for: NSString, NSNumber, NSDate, NSData, NSArray, NSSet, NSDictionary (and their mutable counterparts)? Maybe I'm using the wrong term when I say "Foundation Objects".

Also, the conditional substring takes care of the & – lms Dec 13 '11 at 21:12 @lms: I call all of those minus NSSet the property-list classes. (NSSet isn't a property-list class. ) – Peter Hosey Dec 13 '11 at 21:26.

I'm not sure exactly what you mean here. Are you asking which classes are toll-free bridged?

Depending on what you're trying to do - if you're writing some sort of diagnostic and are just looking to exclude the core classes, you could try just getting the name of the class and looking at the first few letters. If (someObj.class. Description hasPrefix:@"NS") { NSLog(@"Well, it's an NSSomething object..."); }.

This won't flag NSCFString (et al) as CF objects, which is a good demonstration of the folly of trying to sort every object into either the CF bin or the NS bin. – Peter Hosey Dec 13 '11 at 15:30.

You can always ask for it's class. But you should not base you code on that to make some logic decision a path to the dark side, that is... But Seriously, Apple is exposing NSString as a cluster class which mean that under the hood it's a lot of different classes and any one of them could be return to you, and there is no guaranty that in the future they will be the same, the naming could change, etc. Even if I doubt that an old class like NSString would have it's underlaying structure changed, I wouldn't rule that possibility out and place a bet on it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions