tableview store loses reference on ViewDidLoad nested optional unwrapping - json

I am trying to parse some weather data, and I have ran into an issue. I created a default value in an array that gives the user a dummy location and value in a table view. This value is unwrapped using the same code below that I am trying to use for real data. It works fine for the dummy value and can find the locationStore easily, but when I am loading from the view that has the parsed data, the locationStore is showing as nil in the debugger. I am not sure how to solve this, so any help would be greatly appreciated.
class LocationWeatherViewController: UITableViewController{
var locationStore: LocationStore!
var imageStore: ImageStore!
var newLocation: Location!
var locationCreated : NSMutableArray = NSMutableArray( array: ["Test ", 0, 0.0, 0.0 , 0.0])
override func viewDidLoad() {
super.viewDidLoad()
print(locationCreated.count)
if let locationName = locationCreated[0] as? String {
print(locationName)
if let currentTemp = locationCreated[2] as? Double{
print(currentTemp)
if let zip32 = (locationCreated[1] as? Int){
let zip = Int64(zip32)
print(zip)
if let xCrd = locationCreated[3] as? Double{
print(xCrd)
if let yCrd = locationCreated[4] as? Double{
print(yCrd)
newLocation = locationStore.createLocation(false, location: locationName, currentTemp: currentTemp, zipCode: zip, xCord: xCrd, yCord: yCrd)
if let index = locationStore.allLocations.indexOf(newLocation){
let indexPath = NSIndexPath(forRow: index, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}
}
}
}
}
}
func createLocation(random: Bool, location: String, currentTemp: Double, zipCode: Int64, xCord: Double, yCord: Double ) -> Location{
if !random{
let newLocation = Location(random: false, locationNme: location, currentTmperature: currentTemp, locationZpCode: zipCode, xCrd: xCord, yCrd: yCord)
allLocations.append(newLocation)
return newLocation
}
LocationStore:
let newLocation = Location(random: true, locationNme: location, currentTmperature: currentTemp, locationZpCode: zipCode, xCrd: xCord, yCrd: yCord)
allLocations.append(newLocation)
return newLocation
}
Location:
init( locationName: String, currentTemperature: Double, locationZipCode: Int64, xCord: Double, yCord: Double){
self.locationName = locationName
self.currentTemperature = currentTemperature
self.locationZipCode = Int64(locationZipCode)
self.xCord = xCord
self.yCord = yCord
self.itemKey = NSUUID().UUIDString
super.init()
}
convenience init( random:Bool = false, locationNme: String, currentTmperature: Double, locationZpCode: Int64, xCrd: Double, yCrd: Double ){
if random {
let locations = ["Louisville", "Gainesville", "Austin", "San Francisco"]
//38.2527, 85.7585
//29.6516, 82.3248
// 30.2672, -97.7431
// 37.7749, -122.4194
let xCords = [38.2527, 29.6516, 30.2672, 37.7749 ]
let yCords = [-85.7585, -82.3248, -97.7431, -122.4194 ]
let zips = [40217, 32608, 77878, 46454]
let temps = [76.0, 101.3, 95.4, 68.5]
var idx = arc4random_uniform(UInt32(locations.count))
let randomLocation = locations[Int(idx)]
let randomLocationxCord = xCords[Int(idx)]
let randomLocationyCord = yCords[Int(idx)]
let randomZip = zips[Int(idx)]
idx = arc4random_uniform(UInt32(temps.count))
let randomTemp = temps[Int(idx)]
self.init ( locationName: randomLocation, currentTemperature: randomTemp, locationZipCode: Int64(randomZip), xCord: randomLocationxCord, yCord: randomLocationyCord)
}
else{
self.init( locationName: locationNme, currentTemperature: currentTmperature, locationZipCode: locationZpCode, xCord: xCrd, yCord: yCrd)
}
}
APIFinder:
func useAPI(zipCode: Int) -> NSArray{
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let locationURL = NSURL( string: "http://api.openweathermap.org/data/2.5/forecast/city?zip=\(zipCode),us&APPID=74b78f4effe729b2a841cb35e3862d85")
let request = NSURLRequest(URL: locationURL!)
let task = session.dataTaskWithRequest(request) {
(data,response, error) -> Void in
if let locationData = data {
if let jsonString = NSString( data:locationData,encoding: NSUTF8StringEncoding){
let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String: AnyObject]
if let city = json["city"]{
if let name = city["name"]{
self.locationArray[0] = name!
self.locationArray[1] = zipCode
}
if let coord = city["coord"]{
if let xCord = coord!["lat"]{
self.locationArray[3] = xCord!
}
if let yCord = coord!["lon"]{
self.locationArray[4] = yCord!
}
}
}
if let list = json["list"]{
if let main = list[0]["main"]{
if let temp = main!["temp"]{
self.locationArray[2] = temp!
}
}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
else if let requestError = error {
print("Error fetching weather data: \(requestError)")
}
else {
print("Unexpected error with request")
}
}
task.resume()
return locationArray
}
Output:
5
Test
0.0
0
0.0
0.0
5
Hartford
295.54
42328
37.45116
-86.909157
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
Solved:
The problem was that I needed to pass the LocationStore between the views in the segue. Such a dumb problem once the answer was shown.
Thanks for all of the help and suggestions.

Related

Why is my API call not completely reached?

I am currently building a public transportation app in SwiftUI Xcode 12 beta, where you input origin, destination, time of arrival, and date. The idea is to then convert the origin and destination input (private func FetchOriginCoordinates and private func FetchDestCoordinates) into coordinates using an API. This API will then give that data to another API that plans the route.
The problem is that the route planner API doesn't give any output. I have tried printing self.Trips.append(...) but the list is completely empty. I believe this is because the code doesn't reach that part of the code. I have put some print statements in the code in order to understand where the error is occurring. When I call the API my output in the console is this:
Test 2021-04-16 22:33:19.271246+0200 OnTrack2.0[6592:428359] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed Optional(http://api.sl.se/api2/TravelplannerV3_1/trip.json?key=40892db48b394d3a86b2439f9f3800fd&originExtId=300101115&destExtId=300101426&Date=2021-04-18&Time=09:20&searchForArrival=1) [] 10767 bytes Level 1 Level 1.5
This means that the code only reaches the print("Level 1.5") statement. But never the following print statements such as print("Level 2") or print(self.trips). Why is that and how do I solve it?
import Foundation
struct NominationStructure: Decodable {
var lat: String
var lon: String
enum CodingKeys: String, CodingKey {
case lat = "lat"
case lon = "lon"
}
}
struct NearbyStopsStructure: Decodable {
var stopLocationOrCoordLocation: [stopLocationOrCoordLocationStructure]
}
struct stopLocationOrCoordLocationStructure: Decodable {
var StopLocation: StopLocationStructure
}
struct StopLocationStructure: Decodable {
var mainMastExtId: String
}
struct JSONStructure: Decodable {
var Trip: [TripStructure]
}
struct TripStructure: Decodable {
var LegList: LegListStructure
}
struct LegListStructure: Decodable {
var Leg: [LegStructure]
}
struct LegStructure: Decodable {
var Origin: StationStructure
var Destination: StationStructure
var Product: ProductStructure
var name: String
var type: String
var dist: String
}
struct StationStructure: Decodable {
var time: String
var name: String
var date: String
}
struct ProductStructure: Decodable {
var catIn: String
}
struct LocationInfo {
var iD = String()
var input = String()
var lat = String()
var lon = String()
var name = String()
var time = String()
var date = String()
var vehicleType = String()
var transportType = String()
var dist = String()
var legName = String()
}
//One way is to call your functions when you know a step is completed
enum TripFetchStatus: String {
case start
case fetchedOriginCoordinates
case fetchedOriginId
case fetchedDestinationCoordinates
case fetchedDestinationId
case fetchingTrip
case done
case stopped
}
class PlanTripViewModel: ObservableObject {
//Apple frowns upon frezzing a screen so having a way to update the user on what is going on
//When a step is complete have it do something else
#Published var fetchStatus: TripFetchStatus = .stopped{
didSet{
switch fetchStatus {
case .start:
print("Test")
FetchOriginCoordinates { (cors) in
self.origin.lat = cors[0].lat
self.origin.lon = cors[0].lon
self.fetchStatus = .fetchedOriginCoordinates
}
case .fetchedOriginCoordinates:
self.FetchOriginID { (stops) in
self.origin.iD = stops.stopLocationOrCoordLocation[0].StopLocation.mainMastExtId
self.fetchStatus = .fetchedOriginId
}
case .fetchedOriginId:
FetchDestCoordinates { (cors) in
self.dest.lat = cors[0].lat
self.dest.lon = cors[0].lon
self.fetchStatus = .fetchedDestinationCoordinates
}
case .fetchedDestinationCoordinates:
self.FetchDestID { (stops) in
self.dest.iD = stops.stopLocationOrCoordLocation[0].StopLocation.mainMastExtId
self.fetchStatus = .fetchedDestinationId
}
case .fetchedDestinationId:
//Once you have everthing in place then go to the next API
FetchTrip()
case .fetchingTrip:
print("almost done")
case .done:
print("any other code you need to do")
case .stopped:
print("just a filler")
}
}
}
#Published var trip: LocationInfo = LocationInfo()
#Published var dest: LocationInfo = LocationInfo()
#Published var origin: LocationInfo = LocationInfo()
#Published var arrivalTime = String()
#Published var travelDate = String()
#Published var tripIndex = Int()
#Published var Trips: [Dictionary<String, String>] = []
public func FetchTrip() {
Trips.removeAll()
let tripKey = "40892db48b394d3a86b2439f9f3800fd"
let tripUrl = URL(string: "http://api.sl.se/api2/TravelplannerV3_1/trip.json?key=\(tripKey)&originExtId=\(self.origin.iD)&destExtId=\(self.dest.iD)&Date=\(self.travelDate)&Time=\(self.arrivalTime)&searchForArrival=1")
print(tripUrl)
URLSession.shared.dataTask(with: tripUrl!) {data, response, error in
if let data = data {
print(data)
print("Level 1")
do {
print("Level 1.5")
if let decodedJson = try? JSONDecoder().decode(JSONStructure.self, from: data) {
self.tripIndex = decodedJson.Trip.count - 1
print("Level 2")
for i in 0..<decodedJson.Trip[self.tripIndex].LegList.Leg.count {
self.trip.transportType = decodedJson.Trip[self.tripIndex].LegList.Leg[i].type
if self.trip.transportType == "WALK" {
self.origin.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.name
self.origin.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.time.prefix(5))
self.origin.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.date
self.dest.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.name
self.dest.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.time.prefix(5))
self.dest.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.date
self.trip.vehicleType = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Product.catIn
self.trip.dist = decodedJson.Trip[self.tripIndex].LegList.Leg[i].dist
print(self.origin.name)
print(self.dest.name)
self.Trips.append(["Origin": self.origin.name, "Destination": self.dest.name, "OriginTime": self.origin.time, "DestTime": self.dest.time, "OriginDate": self.origin.date, "DestDate": self.dest.date, "TransportType": self.trip.transportType, "VehicleType": self.trip.vehicleType, "Distance": self.trip.dist])
}
else {
self.origin.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.name
self.origin.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.time.prefix(5))
self.origin.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.date
self.dest.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.name
self.dest.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.time.prefix(5))
self.dest.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.date
self.trip.vehicleType = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Product.catIn
self.trip.legName = decodedJson.Trip[self.tripIndex].LegList.Leg[i].name
print(self.origin.name)
print(self.dest.name)
self.Trips.append(["Origin": self.origin.name, "Destination": self.dest.name, "OriginTime": self.origin.time, "DestTime": self.dest.time, "OriginDate": self.origin.date, "DestDate": self.dest.date, "TransportType": self.trip.transportType, "VehicleType": self.trip.vehicleType, "LegName": self.trip.legName])
}
}
}
} catch {
print(error)
}
}
}.resume()
print(self.Trips)
}
//Simple version just to replicate put your code within
private func FetchOriginCoordinates(completion: #escaping ([NominationStructure]) -> ()) {
let scheme = "https"
let host = "nominatim.openstreetmap.org"
let path = "/search"
let queryItemCountry = URLQueryItem(name: "country", value: "Sweden")
let queryItemCity = URLQueryItem(name: "city", value: "Stockholm")
let queryItemStreet = URLQueryItem(name: "street", value: self.origin.input)
let queryItemFormat = URLQueryItem(name: "format", value: "json")
var urlComponents = URLComponents()
urlComponents.scheme = scheme
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = [queryItemCountry, queryItemCity, queryItemStreet, queryItemFormat]
URLSession.shared.dataTask(with: urlComponents.url!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode([NominationStructure].self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
private func FetchDestCoordinates(completion: #escaping ([NominationStructure]) -> ()) {
let scheme = "https"
let host = "nominatim.openstreetmap.org"
let path = "/search"
let queryItemCountry = URLQueryItem(name: "country", value: "Sweden")
let queryItemCity = URLQueryItem(name: "city", value: "Stockholm")
let queryItemStreet = URLQueryItem(name: "street", value: self.dest.input)
let queryItemFormat = URLQueryItem(name: "format", value: "json")
var urlComponents = URLComponents()
urlComponents.scheme = scheme
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = [queryItemCountry, queryItemCity, queryItemStreet, queryItemFormat]
URLSession.shared.dataTask(with: urlComponents.url!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode([NominationStructure].self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
private func FetchOriginID(completion: #escaping (NearbyStopsStructure) -> ()) {
let nearbyStopsKey = "8444f9a2f75f4c27937a7165abd532a0"
let stopIDUrl = URL(string: "http://api.sl.se/api2/nearbystopsv2.json?key=\(nearbyStopsKey)&originCoordLat=\(self.origin.lat)&originCoordLong=\(self.origin.lon)&maxNo=1")
URLSession.shared.dataTask(with: stopIDUrl!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode(NearbyStopsStructure.self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
private func FetchDestID(completion: #escaping (NearbyStopsStructure) -> ()) {
let nearbyStopsKey = "8444f9a2f75f4c27937a7165abd532a0"
let stopIDUrl = URL(string: "http://api.sl.se/api2/nearbystopsv2.json?key=\(nearbyStopsKey)&originCoordLat=\(self.dest.lat)&originCoordLong=\(self.dest.lon)&maxNo=1")
URLSession.shared.dataTask(with: stopIDUrl!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode(NearbyStopsStructure.self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
}

How To Pass An Array Of Data Into A JSON Request Swift

I am trying to take information from a Firebase snapshot of each child within a certain branch of the database and pass it into a function which uses a google API request. However, instead of passing in one lot of information, completing the function, and then doing the same for the next lot of information it is instead passing all the information all at once. This is leading the url only getting one address instead of all of them. I am getting the error "Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444" too.
Code For Getting The Snapshot:
func fetchSalonsForMarkers() {
let ref = Database.database().reference()
let _ = ref.child("user_profile").observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.value as! [String : AnyObject] {
let snapshotChildData = child.value as! [String : AnyObject]
var uniqueSalonData = Salon()
for (key, value) in snapshotChildData {
uniqueSalonData.acceptedTerms = snapshotChildData["acceptedterms"] as? String
uniqueSalonData.acceptedTermsModdifiedAt = snapshotChildData["acceptedtermsmoddifiedat"] as? String
uniqueSalonData.businessName = snapshotChildData["businessname"] as? String
uniqueSalonData.companyAddressCounty = snapshotChildData["companyaddresscounty"] as? String
uniqueSalonData.companyAddressPostCode = snapshotChildData["companyaddresspostcode"] as? String
uniqueSalonData.companyAddressStreet = snapshotChildData["companyaddressstreet"] as? String
uniqueSalonData.companyAddressTown = snapshotChildData["companyaddresstown"] as? String
uniqueSalonData.companyEmail = snapshotChildData["companyemail"] as? String
uniqueSalonData.companyMobilePhone = snapshotChildData["companymobilephone"] as? Int
uniqueSalonData.ownerFirstName = snapshotChildData["ownerFirstName"] as? String
uniqueSalonData.ownerLastName = snapshotChildData["ownerLastName"] as? String
uniqueSalonData.stripeCustomerId = snapshotChildData["stripeCustomerId"] as? String
}
self.salons.append(uniqueSalonData)
self.longLatLookUp(street: uniqueSalonData.companyAddressStreet ?? "Street Error", town: uniqueSalonData.companyAddressTown ?? "Town Error", county: uniqueSalonData.companyAddressCounty ?? "County Error", postcode: uniqueSalonData.companyAddressPostCode ?? "Postcode Error", businessName: uniqueSalonData.businessName ?? "Name Error")
}
})
}
Code For API Call(Google Geocoding)
func longLatLookUp(street: String, town : String, county: String, postcode: String, businessName: String) {
let completeSalonAddress = ("\(street) \(county) Scotland")
let formattedSalonAddress = completeSalonAddress.replacingOccurrences(of: " ", with: "")
let url = URL(string: "https://maps.googleapis.com/maps/api/geocode/json?address=\(formattedSalonAddress)&key=APIKEY")!
//API Key does have valid key.
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
let json = try? JSONDecoder().decode(Welcome.self, from: data)
let salonLat = json?.results[0].geometry.location.lat
let salonLng = json?.results[0].geometry.location.lng
self.addMarkers(lat: salonLat!, lng: salonLng!, businessName: businessName)
}
task.resume()
}
Code For Adding Markers(For Context):
func addMarkers(lat: Double, lng: Double, businessName: String) {
DispatchQueue.main.async {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: lat , longitude: lng)
marker.title = businessName
marker.snippet = "Test"
marker.appearAnimation = GMSMarkerAnimation.pop
let markerPic: UIImage = UIImage(named: "AppIcon")!
marker.icon = markerPic
marker.map = self.mapView
}
}

Networking using JSONDecoder & codable protocol

I am wondering what i am doing wrong . I am trying to understand how to use urlsession and codable protocol using JSONDecoder. When i use JSONDecoder i am getting the following error message :
keyNotFound(CodingKeys(stringValue: "name", intValue: nil), my resaponse contain ''name'' . But when i use JSONSerialization, I am able to print the response . If someone can explain me.
Code using JSONDecoder
struct Business:Codable {
let name: String
enum CodingKeys: String, CodingKey {
case name = "name"
}
init(from decoder: Decoder) throws {
let value = try decoder.container(keyedBy: CodingKeys.self)
self.name = try value.decode(String.self, forKey:CodingKeys.name)
}
}
let task = session.dataTask(with: request) { (data, response, error) in
if let response = response as? HTTPURLResponse {
print(response)
} else{
print("error")
}
guard let data = data else {return}
do {
let business = try JSONDecoder().decode(Business.self, from: data)
print(business.name)
} catch {
print("Error parsing JSON: \(error)")
}
}
task.resume()
Code using JSONSerialization
struct Business: Decodable {
let name: String
let displayAddress: String
let categories: String
let imageUrl : String
init(json: [String:Any]) {
name = json["name"] as? String ?? ""
displayAddress = json["location"] as? String ?? ""
categories = json["categories"] as? String ?? ""
imageUrl = json["image_url"] as? String ?? ""
}
}
let task = session.dataTask(with: request) { (data, response, error) in
if let response = response as? HTTPURLResponse {
print(response)
} else{
print("error")
}
guard let data = data else {return}
do {
if let myjson = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? Dictionary<String,Any> {
print(myjson)
}
} catch {
print("Error parsing ")
}
}
task.resume()
The response
["region": {
center = {
latitude = "43.67428196976998";
longitude = "-79.39682006835938";
};
}, "businesses": <__NSArrayM 0x60000211cff0>(
{
alias = "pai-northern-thai-kitchen-toronto-5";
categories = (
{
alias = thai;
title = Thai;
}
);
coordinates = {
latitude = "43.647866";
longitude = "-79.38864150000001";
};
"display_phone" = "+1 416-901-4724";
distance = "3010.095870925626";
id = "r_BrIgzYcwo1NAuG9dLbpg";
"image_url" = "https://s3-media3.fl.yelpcdn.com/bphoto/t-g4d_vCAgZH_6pCqjaYWQ/o.jpg";
"is_closed" = 0;
location = {
address1 = "18 Duncan Street";
address2 = "";
address3 = "";
city = Toronto;
country = CA;
"display_address" = (
"18 Duncan Street",
"Toronto, ON M5H 3G8",
Canada
);
state = ON;
"zip_code" = "M5H 3G8";
};
name = "Pai Northern Thai Kitchen";
phone = "+14169014724";
price = "$$";
rating = "4.5";
"review_count" = 2405;
transactions = (
);
url = "https://www.yelp.com/biz/pai-northern-thai-kitchen-toronto-5?adjust_creative=A4ydpSOHv8wBNquTDeh0DQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=A4ydpSOHv8wBNquTDeh0DQ";
},
Business is not the root data object in your JSON. You need something like this:
struct Business: Codable {
let name: String
}
struct RootObject: Codable {
let businesses: [Business]
}
let rootObject = try JSONDecoder().decode(RootObject.self, from: data)
print(rootObject.businesses.first?.name)

Loading Serialised JSON Into Table?

Im having a really hard time wrapping my head around this process, I have made an API call and received back JSON, I then use a model to serialise the JSON so it can be used easily in my View Controller Table, but im having issues with how to call the API in the View Controller to have the result fit into my serialisation model. I hope I explained it correctly?
Here is the code of my API Request:
open class ApiService: NSObject {
open func getData(completionHandler: #escaping (NSDictionary?, NSError?) -> Void) -> Self {
let requestUrl = "https://wger.de/api/v2/exercise/?format=json"
Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default)
.responseJSON { response in
switch response.result {
case .success( let data):
completionHandler(data as? NSDictionary, nil)
case .failure(let error):
print("Request failed with error: \(error)")
completionHandler(nil, error as NSError?)
}
}
return self
}
}
and here is the code of my serialisation
final public class Exercise: ResponseObjectSerializable {
var id: Int!
var description: String!
var name: String!
var muscles: String!
var equipment: String!
public init?(response: HTTPURLResponse, representation: Any) {
guard
let representation = representation as? [String: Any],
let id = representation["id"] as? Int,
let description = representation["description"] as? String,
let name = representation["name"] as? String,
let muscles = representation["muscles"] as? String,
let equipment = representation["equipment"] as? String
else { return nil }
self.id = id
self.description = description
self.name = name
self.muscles = muscles
self.equipment = equipment
}
}
But I cant work out how to fit this into my view controller function call which is currently this
let apiService = ApiService()
let searchController = UISearchController(searchResultsController: nil)
var arrRes: [String] = []
var filtered: [String] = []
var searchActive: Bool = false
var id: Int?
var description: String?
var name: String?
var muscles: String?
var equipment: String?
override func viewDidLoad() {
super.viewDidLoad()
exercisesTableView.delegate = self
exercisesTableView.dataSource = self
exerciseSearchBar.delegate = self
getApiData()
}
func getApiData() {
let _ = apiService.getData() {
(data, error) in
if let data = data {
if let arr = data["results"] as? [String] {
self.arrRes = arr
self.exercisesTableView.reloadData()
}
} else if let error = error {
print(error)
}
}
}
First of all the HTTP response does not affect the custom class at all so I left it out.
Second of all the values for keys muscles and equipment are arrays rather than strings.
Third of all since the JSON data seems to be immutable declare the properties in the class as constant (let)
With a few slightly changes this is the custom class
final public class Exercise {
let id : Int
let description: String
let name: String
let muscles : [Int]
let equipment : [Int]
public init?(dictionary: [String: Any]) {
guard
let id = dictionary["id"] as? Int,
let description = dictionary["description"] as? String,
let name = dictionary["name"] as? String,
let muscles = dictionary["muscles"] as? [Int],
let equipment = dictionary["equipment"] as? [Int]
else { return nil }
self.id = id
self.description = description
self.name = name
self.muscles = muscles
self.equipment = equipment
}
}
Then you have to declare the data source array
var exercises = [Exercise]()
And in the method getApiData() populate the array
...
if let results = data["results"] as? [[String:Any]] {
for result in results {
if let exercise = Exercise(dictionary: result) {
self.exercises.append(exercise)
}
}
self.exercisesTableView.reloadData() // might be dispatched on the main thread.
}
Note: Any is used in Swift 3, in Swift 2 replace all occurrences of Any with AnyObject

populating JSON in tableview

So I'm trying to populate multiple tableviews with the json code below:
{ "company":[
{ "company_id":"0",
"name": "Company Name",
"phone_number":"0123978978",
"website":"http:\/\/www.Company.co.uk\/",
"email":"Company#hotmail.co.uk",
"address":"123 Alm Street...",
"employees":[
{
"company_id": "0",
"name":"Steve",
"age":"25",
"description":"desc"},
{
"company_id": "0",
"name":"Paul",
"age":"35",
"description":"desc"}]
}
]
}
below is the model of fetching the json and formatting it into something I can use
class Company: NSObject {
var name: String
var phoneNumber: String
var website: String
var email: String
var address: String
var employees: [Employee]
override init() {
}
init(name: String, phoneNumber: String, website: String, email: String, address: String, employees: [Employee]) {
self.name = name
self.phoneNumber = phoneNumber
self.website = website
self.email = email
self.address = address
self.employees = employees
}
}
class Employee : NSObject {
var name:String
var age:Int
var information:String
override init() {
}
init(name: String, age: Int, information: String) {
self.name = name
self.age = age
self.information = information
}
}
func getJSON(completion: (array: [Company])->()) {
var companyArray = [Company]()
let requestURL: NSURL = NSURL(string: urlString)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
data, response, error -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("Everything is fine, file downloaded successfully.")
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
if let companies = json["companies"] as? NSArray {
for companyDict in companies {
let company = Company()
let employee = Employee()
var employeeArray = [Employee]()
if let name = companyDict["name"] as? String,
let phoneNumber = companyDict["phone_number"] as? String,
let website = companyDict["website"] as? String,
let email = companyDict["email"] as? String,
let address = companyDict["address"] as? String {
company.name = name
company.phoneNumber = phoneNumber
company.website = website
company.email = email
company.address = address
}
if let employees = companyDict["employees"] as? NSArray {
for employeesDict in employees {
if let name = employeesDict["name"] as? String,
let age = employeesDict["age"] as? Int,
let information = employeesDict["information"] as? String {
var employeeArray = [Employee]()
let employee = Employee()
employee.name = name
employee.information = information
employee.age = age
employeeArray.append(employee)
company.employees = employeeArray
}
}
}
companyArray.append(company)
}
dispatch_async(dispatch_get_main_queue()) {
completion(array: companyArray)
}
}
} catch {
print("Error with Json: \(error)")
}
}
}
task.resume()
}
This is where the problem arrises, I can populate my first tableview fine. Each cell displays the data fine. The problem occurs when I try to populate my second tableview based on selection of a cell in my first tableview.
let selectedCompany:Company?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.selectedCompany!.employees!.count //ignore force unwrap
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let employee = self.selectedCompany!.employees![indexPath.row] //ignore force unwrap
cell.employeeName.text = employee.name
return cell
}
The line let employee = self.selectedCompany!.employees![indexPath.row] displays a compiler error of type has no subscript members. I think I know why - I need to populate the second table view as an array. At the moment, Employee is not an array. I've tried several different methods to make it an array but I can't seem to do it. What I've tried usually returns nil and the app crashes or i get compiler errors
You have to declare employees in Company as array
var employees = [Employee]()
and also in the initializer
init(name ... , employees: [Employee]) {
and – very very important – you have to create the Employee instance in the repeat loop, otherwise you have employees.count times the same object (due to reference semantics of classes)
...
for employeesDict in employees {
if let name = employeesDict["name"] as? String,
let age = employeesDict["age"] as? Int,
let information = employeesDict["information"] as? String {
let employee = Employee(name:name, age:age, information:information)
employeeArray.append(employee)
}
}
}
company.employees = employeeArray
...
You have to use the initializer anyway. And you have to assign the array after the repeat loop.