2013-10-07 150 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) –