2013-10-07 149 views
0

我需要在UITextView中显示RTF file。我的代码看起来像下面:错误消息NSData

我.h文件中:

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

我.m文件

@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]; 
} 

此代码给我下面的错误消息:

at

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

,并在

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

如何解决这两个错误?

+1

什么'[NSBundle pathForResource:ofType:]'然后呢?这当然不是“Credits.rtf”的内容...... – trojanfoe

回答

0

[[NSBundle mainBundle] pathForResource:返回类型为NSString,见NSBundle Class Reference,所以你不能直接与NSString NSData的分配,这样你就可以通过返回的URL的NSData用不同的初始化器。

NSAttributedString没有名为initWithRTF的init方法。这就是你看到错误的原因。

3

这种方法:

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

返回路径是你的文件。

如果你有路径后,要读取的文件,使用这样的:

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

在这里找到的文档:

NSBundle Class Reference

NSData Class Reference

而且, NSMutableAttributedString中没有称为InitWithRTF的方法。

+0

感谢您的回答。我可以修复第一个错误。不过,我从这里得到了'initWithRTF'的代码:[developer.apple.com](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/CreatingAttributedStrings.html) –

+0

是的,但该方法适用于Cocoa(OS X),不适用于iOS,对不起 –

+0

'initWithRTF'也适用于iOS:[developer.apple.com](https://developer.apple.com/library/ios /documentation/Cocoa/Conceptual/AttributedStrings/Tasks/RTFAndAttrStrings.html#//apple_ref/doc/uid/20000164-BBCBJIBC) –