[英]'NSInvalidArgumentException' with Hello World program in Swift
I'm working on a simple swift app, using the guide found at this website( http://jamesonquave.com/blog/developing-ios-apps-using-swift-part-4-adding-interactions/ ). 我正在使用此网站上的指南( http://jamesonquave.com/blog/developing-ios-apps-using-swift-part-4-adding-interactions/ )开发一个简单的swift应用程序。 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.
2014-12-30 16:54:52.514 HelloWorld [3058:151440]> Interface Builder文件中的未知类_TtC10HelloWorld14ViewController。 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'
2014-12-30 16:54:52.538 HelloWorld [3058:151440]-[UIViewController tableView:numberOfRowsInSection:]:?>无法识别的选择器已发送到实例0x7fcff07375d0 2014-12-30 16:54:52.542 HelloWorld [3058:151440] * **由于未捕获的异常>'NSInvalidArgumentException'而终止应用程序,原因:'-[UIViewController tableView:numberOfRowsInSection:]:>无法识别的选择器已发送至实例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. 我将添加针对SearchResultsViewController和APIController的代码,其中包含对Google API进行调用的代码。 I also have a table in the Main.Storyboard which is a datastore and delegate for tableData variable in SearchResultsViewController.
我在Main.Storyboard中还有一个表,它是SearchResultsViewController中tableData变量的数据存储和委托。
SearchResultsViewController.swift: 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 APIController.swift:导入基金会
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://itunes.apple.com/search?term=\(escapedSearchTerm)&media=software"
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.
我认为问题出在某些地方,因为我重命名了SearchResultsViewController,并且变量tableData不再正确链接了,但是我没有正确的证据。 Thank you again.
再次感谢你。
Unknown class _TtC10HelloWorld14ViewController in >Interface Builder file
> Interface Builder文件中的未知类_TtC10HelloWorld14ViewController
There's your answer. 有你的答案。 The problem is that you changed the name of the ViewController class, to SearchResultsViewController - but you didn't tell the storyboard about that.
问题是您将ViewController类的名称更改为SearchResultsViewController-但是您没有告诉情节提要。 You need to tell it.
您需要告诉它。 Find that scene and tell it the correct view controller class name in the Identity inspector.
找到该场景,然后在身份检查器中告诉它正确的视图控制器类名称。
I think the problem lies some where in the fact that I renamed SearchResultsViewController
我认为问题出在某些地方,因为我重命名了SearchResultsViewController
Yes, I see now that you already know this. 是的,我现在知道您已经知道这一点。 So just fix it.
所以就解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.