0

This one is a little odd - because I know the canonical answer to it. It's a problem that I'd expect to see on iOS (where NSAttributedString doesn't have initWithRTF), but I'm seeing it on Mac OS X at the moment.

The code is as follows:

+(NSString*)stringFromRTFData:(NSData*)rtfData
{
    #if TARGET_OS_IPHONE
    return nil; //need to extract string from RTF for iOS
    #else
    NSAttributedString* intermediate = [[NSAttributedString alloc]initWithRTF:rtfData documentAttributes:nil];  
    return [intermediate string];
    #endif
}

As you can see, I expect a string to be returned if the code is compiled for Mac OS X, and nil to be returned for iOS (just because I haven't bothered putting the code in for iOS yet. I will. Eventually!)

For most of my projects, this code works perfectly - but on two of them (which are spotlight and quicklook importers) I get the error 'No visible @interface for 'NSAttributedString' declares the selector 'initWithRTF: documentAttributes:'

Furthermore, if I attempt to look up the documentation for this in Xcode it returns the iOS documentation for NSAttributedString, not the OS X documentation for NSAttributedString - suggesting that it thinks this plugin is for iOS. Checking the build settings however shows this project is set to OS X 10.10 with an architecture of 64-bit Intel and a supported platform of OS X.

What gives? Can anyone see my error?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
headbanger
  • 1,038
  • 1
  • 11
  • 32
  • Are you `#import`ing Cocoa or AppKit in the affected translation unit? The `-initWithRTF:documentAttributes:` method is in a category on `NSAttributedString` that's part of AppKit. (The `NSAttributedString` class itself is part of Foundation. So, it might be declared even if its category is not.) – Ken Thomases Dec 26 '14 at 12:45
  • Grr! Sometimes I could throttle myself. Put that down as an answer and I'll give you a tick! – headbanger Dec 26 '14 at 14:39

1 Answers1

3

You need to #import the Cocoa or AppKit header file. Although NSAttributedString is part of Foundation, the -initWithRTF:documentAttributes: method is not. It's part of a category on NSAttributedString in AppKit.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154