Pergunta

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?

Foi útil?

Solução 2

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

Outras dicas

This method:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Credits" ofType:@"rtf"];

Returns the path were your file is.

If, after you have the path, you want to read the file, use this:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];

Find the documentation here:

NSBundle Class Reference

NSData Class Reference

Also, there's no method called InitWithRTF in NSMutableAttributedString.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top