4

(editied to show Swift 1 and Swift 2 code)

I'm trying to read a small text file using Swift 2.2 on Linux (December 22 snapshot).
Mint 14.04 and Ubuntu 15.10 produce identical results.
If there is any way to read from a text file, please answer.

Swift 2 source:

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

error:

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

Swift 1 source:

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

error:

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) ^ 
0

3 Answers 3

2

NSString is not fully implemented yet for the cross platform version of the Foundation framework. You can track the status of the various parts of Foundation here: Foundation Status

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to read a file into a string with the current code?
NSData is listed as mostly implemented. You could try NSData(contentsOfFile: String), but then you'd still need NSString to get the bytes of the NSData object into a string.
I tried using NSData. Same problem - contentsOfFile is not implemented
2

It is also possible to use Glibc function like this

import Glibc let path = "./sample.txt" let BUFSIZE = 1024 let fp = fopen(path, "r") if fp != nil { var buf = [CChar](count:BUFSIZE, repeatedValue:CChar(0)) while fgets(&buf, Int32(BUFSIZE), fp) != nil { print(String.fromCString(buf)!, terminator:"") } } 

Comments

0

try this in Linux Swift 2.2 (January 11, 2016 snapshot with Foundation module) https://swift.org/download/#older-snapshots

import Foundation ... let path = "./sample.txt" if let text = try? NSString(contentsOfFile: path as String, encoding: NSUTF8StringEncoding).bridge() { // use .bridge() to convert to String, see https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Issues.md#known-issues print(text) } 

3 Comments

It looks like Swift 2.2 does not include Foundation, but it will appear in Swift 3.0. The following page shows how to compile a pre-release, which involves building Swift from source. github.com/apple/swift-corelibs-foundation/blob/master/Docs/…
I installed the latest snapshot, and still see the same error. swift-2.2.1-SNAPSHOT-2016-03-28-a-ubuntu14.04/usr/bin/swift
swift 2.2 Linux January 11, 2016 snapshot has Foundation module

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.