문제

Pasteboard에서 발견 된 데이터의 유형을 읽으려고합니다.그러나이 프로그램은 이미지가 있음을 반환하고 있습니다 (아마도 그것이 IF stattment의 첫 번째 조건이기 때문에).내가 도대체 뭘 잘못하고있는 겁니까?이것은 내가 언급하는 코드의 일부입니다.고마워.

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 to nil returning 0 which will also fail the test (as you would want).

다른 팁

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top