无法将数据从文本文件显示到UITextView中

时间:2018-07-02 09:21:43

标签: ios swift uitextview

我无法显示Textfile.txt中的文本。我能够在控制台上成功打印数据,但无法在UITextView上打印。谢谢

 @IBOutlet weak var mytextview: UITextView!    
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let path = "/Users/jack/textfile.txt"

    do {
        var data = try NSString(contentsOfFile: path,
                                encoding: String.Encoding.ascii.rawValue)


        print(data)
    }catch {
        //handle error
        print(error)
    }

    mytextview.text = Data() as? String

}

2 个答案:

答案 0 :(得分:0)

您可以尝试

setRequestHeader

答案 1 :(得分:-1)

尝试一下:

    override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let path = "/Users/jack/textfile.txt"

    do {
        var data = try NSString(contentsOfFile: path,
                                encoding: String.Encoding.ascii.rawValue)

        mytextview.text = data // change here
        print(data)
    }catch {
        //handle error
        print(error)
    }
    mytextview.text = Data() as? String
}