Reputation: 785
Im using Alamofire to pull a JSON file from a server (http://midlandgates.co.uk/JSON/reliveCacheData.json). To do this I have an Alamofire Request function which should pull the data down and write it to a class called JSONDataClass
.
Alamofire Request
Alamofire.request(.GET, "http://midlandgates.co.uk/JSON/reliveCacheData.json")
.response { request, response, data, error in
if let data = data {
let json = JSON(data:data)
for locationData in json {
let locationDataJSON = JSONDataClass(json: locationData.1)
self.cacheData.append(locationDataJSON)
}
for title in self.cacheData {
print(title.memoryTitle)
}
}
}
However my printLn isn't printing the data from the JSON File which indicates there is a problem with either the request or the class. However I'm new to swift and can't seem to work out the problem, all help is appreciated!
Class
import Foundation
import CoreLocation
import SwiftyJSON
class JSONDataClass {
//Location MetaData
var postUser: String!
var memoryTitle: String!
var memoryDescription: String!
var memoryType: String!
var memoryEmotion: String!
var taggedFriends: String!
//Location LocationalData
var memoryLocation: CLLocationCoordinate2D
//MultiMedia AddressData
var media1: String!
var media2: String!
var media3: String!
var media4: String!
var media5: String!
//Writing to varibles
init(json: JSON) {
postUser = json["user"].stringValue
memoryTitle = json["title"].stringValue
memoryDescription = json["description"].stringValue
memoryType = json["type"].stringValue
memoryEmotion = json["emotion"].stringValue
taggedFriends = json["friends"].stringValue
memoryLocation = CLLocationCoordinate2D(latitude: json["latitude"].doubleValue, longitude: json["longitude"].doubleValue)
media1 = json["media1"].stringValue
media2 = json["media2"].stringValue
media3 = json["media3"].stringValue
media4 = json["media4"].stringValue
media5 = json["media5"].stringValue
}
}
Upvotes: 2
Views: 1093
Reputation: 19964
(Note: the author of the JSON content has fixed his JSON based on my answer - my answer was correct at the time of this post).
The problem is that you've got invalid JSON coming back from the server.
I switched your SwiftyJSON code out and wrapped it in a try-catch which illuminated the problem:
Alamofire.request(.GET, "http://midlandgates.co.uk/JSON/reliveCacheData.json")
.response { request, response, data, error in
//print("status \(response)")
do {
let myData = try NSJSONSerialization.JSONObjectWithData(data!,
options: .MutableLeaves)
print(myData)
} catch let error {
print(error)
}
}
And I get the following error:
2015-12-30 13:47:06.129 {{ignore}}[10185:2174510] Unknown class log in Interface Builder file. Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 961." UserInfo={NSDebugDescription=Unescaped control character around character 961.}
This error basically means that you have invalid JSON coming back from the server.
Make sure your json is valid by running it through JSONLint:
Upvotes: 3
Reputation: 17572
After I checked the discussion and the data provided by the server from you link, I can say, that your trouble is how to deal with your JSON (class, or struct, or enum??)
Did you follow some tutorial from SwiftyJSON? While your init(json: JSON) in class JSONDataClass seems to accept some JSON object (aka dictionary), in reality your json is an JSON array! Check SwiftyJSON documentation and take in your account, that you received an array.
try this instead:
for d in json {
let locationDataJSON = JSONDataClass(json: d)
self.cacheData.append(locationDataJSON)
}
and see, that locationDataJSON is NOT location data but one json object from the received array. or just remove .1 in locationData.1
For example:
Alamofire.request(.GET, "http://midlandgates.co.uk/JSON/reliveCacheData.json")
.response { request, response, data, error in
if let data = data {
let json = JSON(data:data)
for locationData in json {
let locationDataJSON = JSONDataClass(json: locationData)
self.cacheData.append(locationDataJSON)
}
for title in self.cacheData {
print(title.memoryTitle)
}
}
}
Upvotes: 2
Reputation: 61238
Are you new to debuggers and breakpoints?
Set a breakpoint in your response closure (for example, in your if let data = data {
line) and step through your code to see if it does what you expect at every stage. Maybe error
is set? Maybe the if let data = data {
conditional failed (you're not doing anything in an else
to log the failure, so how could you know?)...
Upvotes: 2