I am trying to read the type of the data found in the pasteboard. However, the program is returning that I have an image (probably because that is the first condition in the if stattment). What am I doing wrong? THis is the part of the code I am referring to. Thanks.
NSArray * imgType = [NSArray arrayWithObject:[NSImage class]];
NSArray * strType = [NSArray arrayWithObject:[NSString class]];
NSArray * pboardImg = [pboard readObjectsForClasses:imgType
options:nil];
NSArray * pboardStr = [pboard readObjectsForClasses:strType
options:nil];
if( pboardImg ){
// Got an image!
}
if( pboardStr ){
// Got a string!
}
If you are just trying to read the type of the data from the pasteboard, you probably want to use either
-canReadItemWithDataConformingToTypes:or-canReadObjectForClasses:options:in order to just test if these are available.If you want to read the objects themselves, you’re making the right calls, although the way that you are using them might retrieve more than one representation of the same item on the pasteboard in the event that there are multiple items on the pasteboard with both text and image representations.
You might also want to check for
[pboardImg count] > 0. Although the documentation clearly states that nil will be returned if it can’t create any objects of that type, you are unlikely to be able to do anything with a zero-length array anyway and the Objective-C dispatcher will short-circuit the call tonilreturning 0 which will also fail the test (as you would want).