Im a just starting with programming apps in Xcode 7 / Swift 2.0 Im am pretty far with developing my ideas, but I can't seem to get the error handling to work. The viewcontroller it concerns is presenting dates where and when our band plays.
In this Viewcontroller I call Json data from our online server and parse it into a tableview. It all works. But i want the following things to happen too.
- If there is no connection whatsoever (wifi/4G/3G) perform a segue (No Connection)
- If the server or the php script is unreachable, perform a segue (server error )
- If there is no data available (as in Empty Array) Just give a message "There are no dates set."
The Json I get from my PHP script:
(
{
date = "some date";
description = "Some description";
location = "Some location";
others = "some details";
showtime = "some time";
},
{
date = "some date";
description = "Some description";
location = "Some location";
others = "some details";
showtime = "some time";
}
)
This is the ViewController
import UIKit
class GigsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var gigsdata: NSArray = []
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "where_is_header_navigationController.jpg")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
func dataOfJson(url: String) -> NSArray {
let gigsdata = NSData(contentsOfURL: NSURL(string: url)!)
let jsonArray: NSArray = try! NSJSONSerialization.JSONObjectWithData(gigsdata!, options: .MutableContainers) as! NSArray
return jsonArray
}
gigsdata = dataOfJson("http://www.mydomain.eu/app/myscript.php")
}// end of viewDidLoad
// MARK: Table View Delegate Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if gigsdata.count != 0 {
return gigsdata.count
} else {
return 1
}
}
func allowMultipleLines(tableViewCell:UITableViewCell) {
tableViewCell.textLabel?.numberOfLines = 0
tableViewCell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("GigsCell")! as UITableViewCell
// setting the text color
cell.textLabel?.textColor = UIColor.whiteColor()
//Getting the JSON data and turn it into objects
let maingigsdata = (gigsdata[indexPath.row] as! NSDictionary)
//setting constants for the detailview
let gigsDate = maingigsdata["date"] as! String
let gigsLocation = maingigsdata["location"] as! String
// Setting the number of lines per row
cell.textLabel!.numberOfLines = 2
// Filling the cell with data
cell.textLabel!.text = ("\(gigsDate) \n\(gigsLocation)")
// setting the beackground color when selected
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.darkGrayColor()
cell.selectedBackgroundView = backgroundView
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Like i said, im fairly new to this, so please dont go around and name all kinds of proceduresm, functions or things like that. Please don't think that I don't try myself, but 'm stuck now for two weeks.
The thing I saw a lot on videos and tutorials was the DO TRY CATCH thing. But implementing that as good as I can gave me just all kinds of errors, so I must be doing something wrong there.
I hope that there is someone out there who can help me out and make me a lot wiser as I am today!