I have a watchkit table that is populated by information from a JSON. When I run the program the table becomes populated, however it starts to flicker then disappears completely. This happens every time I run it, and sometimes the table just doesn't show up at all. Anyone know why this might be happening?
Edit: Sorry, I should have put my code when I posted this question.
#IBOutlet weak var earthTable: WKInterfaceTable!
//Create cells
private func loadTableData() {
// getEarthquakeInfo is the function that I use to parse the JSON and grab the infomation I need into info.
getEarthquakeInfo { (info) in
self.earthTable.setNumberOfRows(info.count, withRowType: "earthquake")
//Create cells
for index in 0..<self.earthTable.numberOfRows {
var currentRow = self.earthTable.rowControllerAtIndex(index) as earthquakeViewController
let time = info[index].time
let mag = info[index].mag
let title = info[index].title
currentRow.titleLabel.setText("\(title)")
currentRow.timeLabel.setText("\(time)")
currentRow.magLabel.setText("\(mag)")
}
}
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
loadTableData()
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}
Here's my function, getEarthquakeInfo at the top of my interfaceController.swift:
class InterfaceController: WKInterfaceController {
var info = [AppModel]()
func getEarthquakeInfo(completion: (results : [AppModel]) ->Void ){
DataManager.getEarthquakeDataFromFileWithSuccess {
(data) -> Void in
let json = JSON(data: data)
if var JsonArray = json.array {
JsonArray.removeAtIndex(0)
for appDict in JsonArray {
// parsing
var ids: String? = appDict["id"].stringValue
var title: String? = appDict["title"].stringValue
var time: String? = appDict["time"].stringValue
var lattitude: String? = appDict["lat"].stringValue
var longitude: String? = appDict["lng"].stringValue
var north: String? = appDict["north"].stringValue
var west: String? = appDict["west"].stringValue
var mag: String? = appDict["mag"].stringValue
var depth: String? = appDict["depth"].stringValue
var timeStamp: String? = appDict["timestamp"].stringValue
// Splitting up title string into 2 parts
let newString = title!.stringByReplacingOccurrencesOfString(" ", withString: " - ", options: NSStringCompareOptions.LiteralSearch, range: nil)
var title2strings = newString.componentsSeparatedByString(" - ")
var scale = title2strings[0]
var location = title2strings[1]
// replacing M in scale string with Richter Scale
let scaleString = scale.stringByReplacingOccurrencesOfString("ML", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
let scaleString2 = scaleString.stringByReplacingOccurrencesOfString("mb", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
let scaleString3 = scaleString2.stringByReplacingOccurrencesOfString("Mw", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
let scaleString4 = scaleString3.stringByReplacingOccurrencesOfString("MD", withString: "Magnitude", options: NSStringCompareOptions.LiteralSearch, range: nil)
let scaleString5 = scaleString4.stringByReplacingOccurrencesOfString("M ", withString: "Magnitude ", options: NSStringCompareOptions.LiteralSearch, range: nil)
//Formatting the date
var date = NSDate(dateString: time!).getDatePart()
// Collecting all the information
var information = AppModel(idEarth: ids, title: title, time: date, lat: lattitude, lng: longitude, north: north!, west: west, mag: mag, depth: depth, timeStamp: timeStamp, location: location, scale: scaleString5)
self.info.append(information)
//sorting array by highest magnitude
// self.info.sort({$0.mag > $1.mag})
// returning the completion handler
completion(results: self.info)
}
}
}
}
However, I think this is where the problem is at. This is the file where I call the web service. When I debugged it, the data is retrieved however the table crashes once it's populated.
My DataManager.swift file:
import Foundation
let earthquakeURL = "http://www.kuakes.com/json/"
class DataManager {
class func getEarthquakeDataFromFileWithSuccess(success: ((websiteData: NSData) -> Void)) {
//1
loadDataFromURL(NSURL(string: earthquakeURL)!, completion:{(data, error) -> Void in
//2
if let urlData = data {
//3
success(websiteData: urlData) // When I debug, this line is hit and the data is being received, the table populates, however like I said it just disappears completely.
}
else {
println("nothing")
}
})
}
class func loadDataFromURL(url: NSURL, completion:(data: NSData?, error: NSError?) -> Void) {
var session = NSURLSession.sharedSession()
// Use NSURLSession to get data from an NSURL
let loadDataTask = session.dataTaskWithURL(url, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if let responseError = error {
completion(data: nil, error: responseError)
} else if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 200 {
var statusError = NSError(domain:"com.kuakes", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP status code has unexpected value."])
completion(data: nil, error: statusError)
} else {
completion(data: data, error: nil)
}
}
})
loadDataTask.resume()
}
}
Related
Im new to Swift and I'm trying to use the Google Directions API. I have modified a function to get a Polyline and put the Google directions into an array. The array of directions I declared as a property of the class I'm working in so I can display the results in a table. I have checked and the array is be properly populated with the directions inside the closure. However when I try to use it outside the closure in the CellForTableAT function it is empty. I'm probably not getting the data out of the closures correctly but I cant figure it out. I begin building my array at //MARK: Start Building Directions Array using swiftyJason
func getDirections(currentDestination: GMSMarker, origin: String!, destination: String!, waypoints: Array<String>!, mode: String!, completionHandler: ((_ status: String, _ success: Bool) -> Void)?) {
if let originLocation = origin {
if let destinationLocation = destination {
var directionsURLString = baseURLDirections + "origin=" + originLocation + "&destination=" + destinationLocation + "&mode=" + mode
if let routeWaypoints = waypoints {
directionsURLString += "&waypoints=optimize:true"
for waypoint in routeWaypoints {
directionsURLString += "|" + waypoint
}
}
directionsURLString = ("\(directionsURLString)&sensor=true&key=???????????????")
print("directons*******")
print(directionsURLString)
directionsURLString = directionsURLString.addingPercentEscapes(using: String.Encoding.utf8)!
let directionsURL = NSURL(string: directionsURLString)
DispatchQueue.main.async( execute: { () -> Void in
let directionsData = NSData(contentsOf: directionsURL! as URL)
do{
let dictionary: Dictionary<String, AnyObject> = try JSONSerialization.jsonObject(with: directionsData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String, AnyObject>
let status = dictionary["status"] as! String
let json = JSON(data: directionsData as! Data)
if status == "OK" {
self.selectedRoute = (dictionary["routes"] as! Array<Dictionary<String, AnyObject>>)[0]
self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<String, AnyObject>
let legs = self.selectedRoute["legs"] as! Array<Dictionary<String, AnyObject>>
let startLocationDictionary = legs[0]["start_location"] as! Dictionary<String, AnyObject>
self.originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)
let endLocationDictionary = legs[legs.count - 1]["end_location"] as! Dictionary<String, AnyObject>
self.destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)
let originAddress = legs[0]["start_address"] as! String
let destinationAddress = legs[legs.count - 1]["end_address"] as! String
for (index, leg) in json["routes"][0]["legs"].arrayValue.enumerated() {
var count = 0
//MARK: Start Building Directions Array
for (stepIndex, step) in json["routes"][0]["legs"][index]["steps"].arrayValue.enumerated() {
count += 1
let htmlInstructions = json["routes"][0]["legs"][index]["steps"][stepIndex]["html_instructions"].string
let distance = json["routes"][0]["legs"][index]["steps"][stepIndex]["distance"]["text"].string
let duration = json["routes"][0]["legs"][index]["steps"][stepIndex]["duration"]["text"].string
let direction:Direction = Direction(index: count, htmlInstructions: htmlInstructions, distance: distance, duration: duration)
self.directions.append(direction)
}
self.tableView.reloadData()
}
//end of stepts to get writtine directions
//NOT Plotting markers endpoins
//position markers for ployline endpoints
//let originMarker = GMSMarker(position: self.originCoordinate)
// originMarker.map = self.mapView
//originMarker.icon = UIImage(named: "mapIcon")
// originMarker.title = originAddress
self.destinationMarker = currentDestination
// destinationMarker.map = self.mapView
// destinationMarker.icon = UIImage(named: "mapIcon")
// destinationMarker.title = destinationAddress
// destinationMarker.icon = GMSMarker.markerImage(with: UIColor.green)
if waypoints != nil && waypoints.count > 0 {
for waypoint in waypoints {
let lat: Double = (waypoint.components(separatedBy: ",")[0] as NSString).doubleValue
let lng: Double = (waypoint.components(separatedBy: ",")[1] as NSString).doubleValue
let marker = GMSMarker(position: CLLocationCoordinate2DMake(lat, lng))
marker.map = self.mapView
marker.icon = UIImage(named: "mapIcon")
}
}
self.routePolyline.map = nil
let route = self.overviewPolyline["points"] as! String
let path: GMSPath = GMSPath(fromEncodedPath: route)!
self.routePolyline = GMSPolyline(path: path)
self.routePolyline.map = self.mapView
self.routePolyline.strokeColor = UIColor.red
self.routePolyline.strokeWidth = 3.0
//Fit map to entire polyline
var bounds = GMSCoordinateBounds()
for index in 1...path.count() {
bounds = bounds.includingCoordinate(path.coordinate(at: index))
}
self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
// end of fit map to ployline
}
else {
print("status of poly draw")
//completionHandler(status: status, success: false)
}
}
catch {
print("catch")
// completionHandler(status: "", success: false)
}
})
}
else {
print("Destination is nil.")
//completionHandler(status: "Destination is nil.", success: false)
}
}
else {
print("Origin is nil")
//completionHandler(status: "Origin is nil", success: false)
}
}
I added a return (->([Direction]))to the closure and was able to access the data outside of it.
func getDirections(currentDestination: GMSMarker, origin: String!, destination: String!, waypoints: Array<String>!, mode: String!, completionHandler: ((_ status: String, _ success: Bool) -> Void)?) -> ([Direction])
I'm new so not sure if its best practice to do it that way, but I needed data to display in a table.
When I load my table then activate the search, I can get the search data in my tableview. To make my app faster, I create a seperate view only for search. But I couldn't get my tableview populated with search data. I can see I get the data correctly with print(data), but my table is not populated.
class YazarTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
var searchResults: [JSON]? = []
var searchController = UISearchController()
#IBOutlet weak var searchButton: UIBarButtonItem!
#IBAction func searchButtonTouched(sender: AnyObject) {
showSearchBar()
}
func showSearchBar() {
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.searchBar.delegate = self
controller.dimsBackgroundDuringPresentation = true
controller.searchBar.alpha = 0
navigationItem.setLeftBarButtonItem(nil, animated: true)
UIView.animateWithDuration(0.5, animations: {
controller.searchBar.alpha = 1
}, completion: { finished in
controller.searchBar.becomeFirstResponder()
})
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
}
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
self.searchResults?.removeAll(keepCapacity: false)
if (!searchController.searchBar.text!.isEmpty){
let parameters = ["q" : searchController.searchBar.text]
var searchURL = "http://apilink.com/" + String(parameters) + "/1"
searchURL = searchURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
Alamofire.request(.GET, searchURL, encoding: .JSON)
.responseJSON{ response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling GET on /posts/1")
print(response.result.error!)
return
}
if let value: AnyObject = response.result.value {
let post = JSON(value)
if let data = post[].arrayValue as [JSON]?{
self.searchResults = data
self.tableView.reloadData()
print(data)
}
}
}
}
}
I have a function called getEarthquake() that parses JSON using SwiftyJSON and returns all of the organized information (such as title, magnitude, and time) into an NSMutableArray called info.
var info = NSMutableArray()
func getEarthquake(completion: (results : NSMutableArray) ->Void) {
DataManager.getEarthquakeDataFromFileWithSuccess {
(data) -> Void in
let json = JSON(data: data)
if var JsonArray = json.array {
JsonArray.removeAtIndex(0)
for appDict in JsonArray {
var mag: String? = appDict["mag"].stringValue
var title: String? = appDict["title"].stringValue
var time: String? = appDict["time"].stringValue
var information = AppModel(title: title, magnitude: mag, time1: time)
info.addObject(information)
// info.removeRange(3...48)
completion(results: info)
}
}
}
}
I created another function called getEarthquake2() which calls getEarthquake() and retrieves info. In getEarthquake2() I want it to return only title1, which is a String. However my attempts result only in title1 being nil by the time is it returned.
func getEarthquake2()->String? {
var title1: String?
getEarthquake{ (info) in
var title = info[0].title
title1 = title
}
return title1
}
Can someone guide me in the right direction upon making getEarthquake2()successfully return title1 that doesn't return nil? (I'm sure it's not a matter of Info being nil, as it gets populated at the end of getEarthquake().)
My AppModel.swift file where I can easily organize my code:
import Foundation
class AppModel: NSObject, Printable {
let title: String
let magnitude: String
let time1: String
override var description: String {
return "TITLE: \(title), TIME: \(time1), MAG: \(magnitude)"
}
init(title: String?, magnitude: String?, time1: String?) {
self.title = title ?? ""
self.time1 = time1 ?? ""
self.magnitude = magnitude ?? ""
}
}
My DataManager.swift file where I call the web service:
import Foundation
let earthquakeURL = "http://www.kuakes.com/json/"
class DataManager {
class func getEarthquakeDataFromFileWithSuccess(success: ((websiteData: NSData) -> Void)) {
//1
loadDataFromURL(NSURL(string: earthquakeURL)!, completion:{(data, error) -> Void in
//2
if let urlData = data {
//3
success(websiteData: urlData)
}
else {
println("nothing")
}
})
}
class func loadDataFromURL(url: NSURL, completion:(data: NSData?, error: NSError?) -> Void) {
var session = NSURLSession.sharedSession()
// Use NSURLSession to get data from an NSURL
let loadDataTask = session.dataTaskWithURL(url, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if let responseError = error {
completion(data: nil, error: responseError)
} else if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 200 {
var statusError = NSError(domain:"com.kuakes", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP status code has unexpected value."])
completion(data: nil, error: statusError)
} else {
completion(data: data, error: nil)
}
}
})
loadDataTask.resume()
}
}
In getEarthquake2 you forget to cast your object to AppModel:
func getEarthquake2() -> String? {
var title1: String?
getEarthquake { info in
let title = (info[0] as! AppModel).title
title1 = title
}
return title1
}
I have a function that grabs information from a web service using SwiftyJSON. When I call the service using my DataManager class I am able to populate my array successfully and when I print it out it gives me what I want. However, I cannot return the populated array from my function because I think it's out of scope by the end of the function. Can anyone tell me how I can make my function return my populated array without being out of scope? If perhaps something else I'm doing wrong, any help would be appreciated.
func getEarthquakeInfo()->NSArray {
var info = [AppModel]()
DataManager.getEarthquakeDataFromFileWithSuccess {
(data) -> Void in
let json = JSON(data: data)
if let JsonArray = json.array {
for appDict in JsonArray {
var ids: String? = appDict["id"].stringValue
var title: String? = appDict["title"].stringValue
var time: String? = appDict["time"].stringValue
var information = AppModel(idEarth: ids, title: title, time: time)
info.append(information)
}
}
println("\(info)") // will print the results that I need
}
return info // This is an empty array
}
This is a sample of what I get when it prints out apps:
ID: 146052, TITLE: M 1.7 Explosion - 0km E of Granite Falls, Washington, TIME: 2015-04-13 21:29:40 UTC,
DataManager4.swift file:
let earthquakeURL = "http://www.kuakes.com/json/"
class DataManager {
class func getEarthquakeDataFromFileWithSuccess(success: ((websiteData: NSData) -> Void)) {
//1
loadDataFromURL(NSURL(string: earthquakeURL)!, completion:{(data, error) -> Void in
//2
if let urlData = data {
//3
success(websiteData: urlData)
}
})
}
class func loadDataFromURL(url: NSURL, completion:(data: NSData?, error: NSError?) -> Void) {
var session = NSURLSession.sharedSession()
// Use NSURLSession to get data from an NSURL
let loadDataTask = session.dataTaskWithURL(url, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if let responseError = error {
completion(data: nil, error: responseError)
} else if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 200 {
var statusError = NSError(domain:"com.kuakes", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP status code has unexpected value."])
completion(data: nil, error: statusError)
} else {
completion(data: data, error: nil)
}
}
})
loadDataTask.resume()
}
}
Here's how I solved it:
var info = [AppModel]()
func getEarthquakeInfo(completion: (results : NSArray?) ->Void ){
DataManager.getEarthquakeDataFromFileWithSuccess {
(data) -> Void in
let json = JSON(data: data)
if let JsonArray = json.array {
for appDict in JsonArray {
var ids: String? = appDict["id"].stringValue
var title: String? = appDict["title"].stringValue
var time: String? = appDict["time"].stringValue
var information = AppModel(idEarth: ids, title: title, time: time)
self.info.append(information)
completion(results: self.info)
}
}
}
}
I'm using the google places api to search for nearby places. However, I only want places of specific types. The code (seen below) works when I specify just one type, but when I add a second my code runs and promptly give me a EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) error on this line:
session.dataTaskWithURL(url!, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
I know the url is valid. I can plug it into the browser and see the json, so I don't understand what the problem is.
func search(location : CLLocationCoordinate2D, radius : Int, callback : (items : [Attraction]?, errorDescription : String?) -> Void) {
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY"
var url = NSURL(string: urlString)
var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithURL(url!, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
if error != nil {
callback(items: nil, errorDescription: error.localizedDescription)
}
if let statusCode = response as? NSHTTPURLResponse {
if statusCode.statusCode != 200 {
callback(items: nil, errorDescription: "Could not continue. HTTP Status Code was \(statusCode)")
}
}
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
callback(items: GooglePlaces.parseFromData(data), errorDescription: nil)
})
}).resume()
}
class func parseFromData(data : NSData) -> [Attraction] {
var attractions = [Attraction]()
var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var results = json["results"] as? [NSDictionary]
for result in results! {
var placeId = result["place_id"] as String
var image = result["icon"] as String
var name = result["name"] as String
var ratingString = ""
var types = result["types"] as [String]
println(types)
if result["rating"] != nil {
var rating = result["rating"] as Double
ratingString = "\(rating)"
}
var coordinate : CLLocationCoordinate2D!
if let geometry = result["geometry"] as? NSDictionary {
if let location = geometry["location"] as? NSDictionary {
var lat = location["lat"] as CLLocationDegrees
var long = location["lng"] as CLLocationDegrees
coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
var placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
var attraction = Attraction(id: placeId, imageUrl: "image url", locationName: name, ratingAvg: "\(ratingString)", types: types, placemarker: placemark)
attractions.append(attraction)
}
}
}
return attractions
}
I know the url is valid
The URL is not valid. You do not know what you think you know. Listen to the runtime. It knows more than you do.
Just try this code alone (in a playground, for instance):
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY"
let url = NSURL(string:urlString)
url is nil. And that's your problem. You cannot force-unwrap nil; you will crash if you do.
Once you acknowledge this, you can start to think about why the URL is not valid. (It's pretty obvious why that might be.) Learning to believe the compiler and the runtime is key to successful programming.
HINT: Form your URL like this and all is well:
let url2 = NSURL(scheme: "https", host: "maps.googleapis.com", path: "/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY")
Why do you suppose that is? Look at the docs and see what this initializer does for you...
I would use .stringByAddingPercentEscapesUsingEncoding()
var urlString = "http://example.com/?foo=bar|baz"
if var escapedURLString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
NSURL(string: escapedURLString)
}
Returns: http://example.com/?foo=bar%7Cbaz