gpt4 book ai didi

linux - Swift 2.2 Linux - 打开文件进行读取

转载 作者:IT王子 更新时间:2023-10-29 01:21:06 26 4
gpt4 key购买 nike

(经过编辑以显示 Swift 1 和 Swift 2 代码)

我正在尝试在 Linux 上使用 Swift 2.2 读取一个小文本文件(12 月 22 日快照)。
Mint 14.04 和 Ubuntu 15.10 产生相同的结果。
如果有任何方法可以从文本文件中读取,请回答。

swift 2 来源:

let text = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
print(text)

错误:

prefix.swift:18:13: error: type 'String' has no member 'stringWithContentsOfFile'
let text = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
^~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~

swift 1 来源:

import Foundation
let text = NSString(contentsOfFile: "foo.txt", encoding: NSASCIIStringEncoding, error: nil)
print(text)

错误:

prefix.swift:14:12: error: argument labels '(contentsOfFile:, encoding:, error:)' do not match any available overloads
let text = NSString(contentsOfFile: "foo.txt", encoding: NSASCIIStringEncoding, error: nil)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prefix.swift:14:12: note: overloads for 'NSString' exist with these partially matching parameter lists: (charactersNoCopy: UnsafeMutablePointer<unichar>, length: Int, freeWhenDone: Bool), (format: String, locale: AnyObject?, arguments: CVaListPointer), (bytes: UnsafePointer<Void>, length: Int, encoding: UInt)
var text = NSString(contentsOfFile: "foo.txt", encoding: NSASCIIStringEncoding, error: nil)
^

最佳答案

对于 Foundation 框架的跨平台版本,NSString 尚未完全实现。您可以在此处跟踪 Foundation 各个部分的状态:Foundation Status

关于linux - Swift 2.2 Linux - 打开文件进行读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34296614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com