Reading a RTF file and parse plaintext

后端 未结 1 1142
梦毁少年i
梦毁少年i 2021-01-28 02:11

I am doing this to get the content of a file using NSOpenPanel, but I am getting a lot of weird stuff returned from the .rtf file I select.

My panel code:



        
相关标签:
1条回答
  • 2021-01-28 02:58

    That "other stuff" is the actual RTF data.

    I would recommend using the NSAttributedString initializer NSAttributedString(path:documentAttributes:), which will read in and process the RTF data. Then you can access the plain text by using attributedString.string.

    In your case, it would be

    if let content = NSAttributedString(path: expandedPath, documentAttributes: nil) {
        // do something with content or content.string
    }
    
    0 讨论(0)
提交回复
热议问题