Changjun Yao
Changjun Yao

Reputation: 39

NSArray with contents of file not work in ios

I am trying to get a JSON array from a JSON file in iOS. The problem is the code is not working The content in JSON file is:

[{a:"m",data:"1597,144",tick:1},{a:"m",data:"1595,144",tick:1}, ...]

This is my Swift code:

  if let filePath = NSBundle.mainBundle().pathForResource("data", ofType: "json") {
     let array = NSMutableArray(contentsOfFile: filePath)
     println("\(array)")
  }

The file exists at path but for some reason its not working. I get null when I try to print the array.

Upvotes: 1

Views: 1961

Answers (1)

Kirsteins
Kirsteins

Reputation: 27335

NSMutableArray(contentsOfFile: filePath) will only work with property lists:

https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

What you have is JSON string. If you want to load array from JSON file check out this: How do I parse JSON from a file in iOS?

Upvotes: 2

Related Questions