Jeppe Christensen Jeppe Christensen - 1 month ago 18
Swift Question

REST and swift, entities are nil

So i have the following xml file here from which i want to get the information and display it inside my application.

I have made the following code:

class Parsexml: NSObject {


var address: String?
var category: String?
var city: String?
var name: String?

override init()
{
}

init(address: String, category: String, city: String, name: String){
self.address = address
self.category = category
self.city = city
self.name = name
}
override var description: String {
return "address: \(address), category: \(category), city: \(city), name: \(name)"
}
}

protocol HomeModelProtocal: class {
func itemsdownloaded(items:NSArray)
}

class HomeModel: NSObject, URLSessionDataDelegate {
weak var delegate: HomeModelProtocal!

var data: NSMutableData = NSMutableData()

let urlpath: String = "http://bkrt.dk/api/place"
}


So far, it complies without errors, and i tried to make a simple call to my class and make it print in the console, in order to see if i get any values from my xml.

However, all my constructors are null.

What am i missing?

Answer

This is a json file not xml. You could do something like this:

if let data = NSData(contentsOfURL: NSURL(string: "http://www.bkrt.dk/api/place")!) {
   let json = try NSJSONSerialization.JSONObjectWithData(data, options:.AllowFragments)
   if let items = json as? [AnyObject] {
      for item in items {
         print(item)
         // init ur object
      }
   }
}

Also you could use third party lib for json parsing.