vendredi 2 janvier 2015

'NSInvalidArgumentException' with Hello World program in Swift

I'm working on a simple swift app, using the guide found at this website(http://ift.tt/1pP9DQF). I'm at the forth step as shown above, and my code compiles but I get an exception when I actually run the code. This is the top of the exception:



2014-12-30 16:54:52.514 HelloWorld[3058:151440] Unknown class _TtC10HelloWorld14ViewController in >Interface Builder file. Hello World! 2014-12-30 16:54:52.538 HelloWorld[3058:151440] -[UIViewController tableView:numberOfRowsInSection:]: ?>unrecognized selector sent to instance 0x7fcff07375d0 2014-12-30 16:54:52.542 HelloWorld[3058:151440] *** Terminating app due to uncaught exception >'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: >unrecognized selector sent to instance 0x7fcff07375d0'



I will add the code I have for a SearchResultsViewController and an APIController which contains code to make a call to the google API. I also have a table in the Main.Storyboard which is a datastore and delegate for tableData variable in SearchResultsViewController.


SearchResultsViewController.swift:



import UIKit

class SearchResultsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, APIControllerProtocol {

@IBOutlet var appsTableView : UITableView?
var tableData = []
var api = APIController()

override func viewDidLoad() {
super.viewDidLoad()
api.searchItunesFor("Angry Birds")
api.delegate = self
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary

cell.textLabel?.text = rowData["trackName"] as? String

// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
let urlString: NSString = rowData["artworkUrl60"] as NSString
let imgURL: NSURL? = NSURL(string: urlString)

// Download an NSData representation of the image at the URL
let imgData = NSData(contentsOfURL: imgURL!)
cell.imageView?.image = UIImage(data: imgData)

// Get the formatted price string for display in the subtitle
let formattedPrice: NSString = rowData["formattedPrice"] as NSString

cell.detailTextLabel?.text = formattedPrice

return cell
}

func didReceiveAPIResults(results: NSDictionary) {
var resultsArr: NSArray = results["results"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.tableData = resultsArr
self.appsTableView!.reloadData()
})
}

}


APIController.swift: import Foundation



protocol APIControllerProtocol {
func didReceiveAPIResults(results: NSDictionary)
}

class APIController {

var delegate: APIControllerProtocol?

init() {
}

func searchItunesFor(searchTerm: String) {

// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)

// Now escape anything else that isn't URL-friendly
if let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
let urlPath = "http://ift.tt/1I6a5i3"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
println("Task completed")
if(error != nil) {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?

var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if(err != nil) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
let results: NSArray = jsonResult["results"] as NSArray
self.delegate?.didReceiveAPIResults(jsonResult)
})

task.resume()
}
}

}


Any suggestions or help would be appreciated, I've spent a couple hours trying to solve this and searching for an answer but to no avail. I think the problem lies some where in the fact that I renamed SearchResultsViewController and that the variable tableData isn't properly linked anymore but I have no evidence that is correct. Thank you again.




Aucun commentaire:

Enregistrer un commentaire