[英]How to find the text in an NSTextView?
我需要在NSTextView
找到文本,並將其保存到文件中。 我可以保存罰款。 到目前為止,我使用了-stringValue
和-textStorage
,但兩者都沒有用。 當我把-stringValue
放進-stringValue
的時候,它只給了我(null),當我把-textStorage
放進-textStorage
的時候,它給了我很大一部分(就像一段長長的隱形文本)。
如何將NSTextView
的文本放入NSString
?
嘗試
NSString *text = [[myTextView textStorage] string];
NSTextStorage繼承自NSMutableAttributedString ,后者繼承自NSAttributedString 。 后者實現了字符串方法。
如果您只是查看文檔中的NSTextView實例方法,這不是太明顯。
試試這個:
[myTextView string];
如果您正在努力將textView stringValue寫入文件,請嘗試以下方法:
[[myTextView string] writeToFile:@"someFile" atomically:YES encoding:NSUnicodeStringEncoding error:&error];
希望這可以幫助!
設置值時,使用text.string = @"XXXX";
獲取值時,使用STRING = [text.string copy];
因為如果忘記了復制, mutableString
將被傳遞給STRING
。 當您使用單個textview
編輯多個內容時,這將引發錯誤。
為了用這個問題為其他任何人編譯所有信息,我正在回答我自己的問題。 如果我不被允許這樣做,請告訴我。
代碼實際上非常簡單。
Files.h
#import <Cocoa/Cocoa.h>
@interface Files : NSObject {
IBOutlet id dataToSave;
}
- (IBAction) saveData: (id) sender;
Files.m
@implementation Files
- (IBAction) saveData: (id) sender; {
NSString *text = [[dataToSave textStorage] string];
NSLog(@"%@", text);
[text writeToFile:@"Users/justin/Desktop/test.txt atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
而不是投票,投票給另外兩個回答這個問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.