NSPasteboard not returning the correct data type
-
14-11-2019 - |
Question
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!
}
Solution
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 to nil
returning 0 which will also fail the test (as you would want).
OTHER TIPS
I have found a solution to this and as gaige said, the data returned contains not only the content that a user can see but many other kinds of information.
I have also found and experimented using other types of information available, some of which are the following:
*NSStringPboardType;
*NSFilenamesPboardType;
*NSTIFFPboardType;
*NSRTFPboardType;
...
However Xcode version that I am using states that some of these might be removed in future versions (am using xcode 3) so if anyone uses these be careful..
Thanks for the help!