Search code examples
iosxcodensdatartf

Error Message NSData


I need to display an RTF file in a UITextView. My code looks like below:

My .h file:

@property (strong, nonatomic) IBOutlet UITextView *creditsTextView;

My .m file

@synthesize creditsTextView;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *creditsData = [[NSBundle mainBundle] pathForResource:@"Credits" ofType:@"rtf"];

    NSAttributedString *attrString;
    NSDictionary *docAttributes;

    attrString = [[NSAttributedString alloc]
                  initWithRTF: creditsData documentAttributes: &docAttributes];
}

This code gives me the following Error messages:

at

*creditsData : Incompatible pointer types 'NSData *' with expression of type 'NSString *'

and at

initWithRTF : No visible @interface for 'NSAttributedString' declares the selector 'initWithRTF:documentAttributes:'

How can I fix these two errors?


Solution

  • [[NSBundle mainBundle] pathForResource: returns type NSString, see NSBundle Class Reference, so you cannot directly assign NSData with NSString, so you can pass the returned URL to NSData with a different initialiser.

    And NSAttributedString does not have an init method called initWithRTF. These are why you are seeing the errors.