0

I want to read a file from Disk in a swift file. It can be a relative or direct path, that doesn't matter. How can I do that?

I've been playing with something like this

let classesData = NSData .dataWithContentsOfMappedFile("path/to/classes.json");

And it finds the file (i.e. doesn't return nil) but I don't know how to manipulate and convert to JSON, the data returned. It isn't in a string format and String() isn't working on it.

2 Answers 2

2

You need to call NSJsonSerialization.JSONObjectWithData:

var error : NSError?
var json = NSJSONSerialization.JSONObjectWithData(classesData, options: nil, error: &error)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use good old Cocoa classes.

let p1  =   "/path/to/any.json"
let d1  =   NSData(contentsOfFile: p1)
let a1 : AnyObject! =   NSJSONSerialization.JSONObjectWithData(d1, options: NSJSONReadingOptions(0), error: nil)

println(a1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.