EDIT: SOLVED
I trying to learn Swift, but just can't seem to get my head around the variation on the syntax.
In my project bundle I've included a file called 'index.text'
In Obj-C I would get it's contents with
So ... How do I now do this in Swift?
I found that the index.text wasn't been copied into the build app, so the file was always coming up nil - once that was corrected the file was then read properly.
I also changed the way it's read in...
So I had it correct from the start, just a different default project build phases setting caused the file not to be included.
I trying to learn Swift, but just can't seem to get my head around the variation on the syntax.
In my project bundle I've included a file called 'index.text'
In Obj-C I would get it's contents with
Code:
NSString indexContent = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"text"]] encoding:NSUTF8StringEncoding error:NULL ]];
So ... How do I now do this in Swift?
I found that the index.text wasn't been copied into the build app, so the file was always coming up nil - once that was corrected the file was then read properly.
I also changed the way it's read in...
Code:
let path = NSBundle.mainBundle().pathForResource("index", ofType: "text")
var possibleContent = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
if let text = possibleContent {
println(text)
}
So I had it correct from the start, just a different default project build phases setting caused the file not to be included.
Last edited: