Pregunta

Estoy tratando de leer el tipo de los datos que se encuentran en la tabla de cartón.Sin embargo, el programa está regresando que tengo una imagen (probablemente porque esa es la primera condición en el estancamiento de Si).¿Qué estoy haciendo mal?Esta es la parte del código en el que me refiero.Gracias.

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!
}

¿Fue útil?

Solución

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).

Otros consejos

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!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top