Ios 没有生成错误,但在模拟器中崩溃。
我没有得到构建错误,但一旦我在模拟器中按下右上角的+键添加新的url,程序就会崩溃。这是我得到的错误: 2017-02-21 09:37:48.667收藏夹网站[2211:128142]->[Favorite_网站。MasterViewController AddButton按下:]:发送到实例0x7ff266408aa0的无法识别的选择器 2017-02-21 09:37:48.672收藏夹网站[2211:128142]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[Favorite_Websites.MasterViewController AddButton Pressed:::]:未识别的选择器发送到实例0x7ff266408aa0' *第一次抛出调用堆栈: ( 0 CoreFoundation 0x000000010d0b0d4b异常预处理+171 1 libobjc.A.dylib 0x000000010cb1221e objc_异常_抛出+48 2 CoreFoundation 0x000000010d120f04-[NSObject(NSObject)不识别选择器:][132 3 CoreFoundation 0x000000010d036005\uuuuuuuuu转发 4 CoreFoundation 0x000000010d035b88\u CF\u转发\u准备\u 0+120 5 UIKit 0x000000010d4d68bc-[UIApplication sendAction:to:from:forEvent:+83 6 UIKit 0x000000010d9184a1-[UIBarButtonItem(UIInternal)\发送操作:withEvent:+149 7 UIKit 0x000000010d4d68bc-[UIApplication sendAction:to:from:forEvent:+83 8 UIKit 0x000000010d65cc38-[UIControl发送操作:发送到:forEvent:+67 9 UIKit 0x000000010d65cf51-[UIControl\u发送操作预防:withEvent:+444 10 UIKit 0x000000010d65d0db-[UIControl\u发送操作预防:带事件:][838 11 UIKit 0x000000010d65be4d-[UIControl触摸附加:withEvent:][668 12 UIKit 0x000000010d544545-[UIWindow\u sendTouchesForEvent:+2747 13 UIKit 0x000000010d545c33-[UIWindow sendEvent:+4011 14 UIKit 0x000000010d4f29ab-[UIApplication sendEvent:+371 15 UIKit 0x000000010dcdf72dDispatchPreprecedEventFromeEventQueue+3248 16 UIKit 0x000000010dcd8463_u handleEventQueue+4879 17 CoreFoundation 0x000000010d055761\uu CFRUNLOOP\u正在调用\u OUT\u以执行\u SOURCE0\u函数+17 18 CoreFoundation 0x000000010d03a98c\uu CFRunLoopDoSources0+556 19 CoreFoundation 0x000000010d039e76 _ucfrunlooprun+918 20 CoreFoundation 0x000000010d039884 CFRunLoopRunSpecific+420 21图形服务0x000000011173fa6f GSEventRunModal+161 22 UIKit 0x000000010d4d4c68 UIApplicationMain+159 23个常用网站0x000000010c516e0f main+111 24 libdyld.dylib 0x00000001107af68d启动+1 ) libc++abi.dylib:以NSException类型的未捕获异常终止 (lldb) 以下是错误来源的MasterViewController.swift代码Ios 没有生成错误,但在模拟器中崩溃。,ios,swift,ios-simulator,xcode8.2,Ios,Swift,Ios Simulator,Xcode8.2,我没有得到构建错误,但一旦我在模拟器中按下右上角的+键添加新的url,程序就会崩溃。这是我得到的错误: 2017-02-21 09:37:48.667收藏夹网站[2211:128142]->[Favorite_网站。MasterViewController AddButton按下:]:发送到实例0x7ff266408aa0的无法识别的选择器 2017-02-21 09:37:48.672收藏夹网站[2211:128142]*由于未捕获的异常“NSInvalidArgumentException”
class MasterViewController: UITableViewController, ModelDelegate {
var detailViewController: DetailViewController? = nil
var model: Model! = nil // manages the app's data
// conform to ModelDelegate protocol; updates view wehn model changes
func modelDataChanged(){
tableView.reloadData() // reload the UITableView
}
// called after the view loads for further UI configuration
override func viewDidLoad() {
super.viewDidLoad()
// set up left and right UIBarButtonItems
self.navigationItem.leftBarButtonItem = self.editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: Selector(("addButtonPressed:")))
self.navigationItem.rightBarButtonItem = addButton
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
model = Model(delegate: self) // create the Model
model.synchronize() // tell model to sync its data
}
// displays a UIAlertController to obtain new url from user
func addButtonPressed(sender: AnyObject) {
displayAddEditurlSearchAlert(isNew: true, index: nil)
}
// handles long press for editing or sharing a url
func tableViewCellLongPressed(
sender: UILongPressGestureRecognizer){
if sender.state == UIGestureRecognizerState.began &&
!tableView.isEditing {
let cell = sender.view as! UITableViewCell // get cell
if let indexPath = tableView.indexPath(for: cell) {
displayLongPressOptions(row: indexPath.row)
}
}
}
// displays the edit/share options
func displayLongPressOptions(row: Int){
// create UIAlertController for user input
let alertController = UIAlertController(title: "Options",
message: "Edit or Share your url",
preferredStyle: UIAlertControllerStyle.alert)
// create Cancel action
let cancelAction = UIAlertAction(title: "Cancel",
style: UIAlertActionStyle.cancel, handler: nil)
alertController.addAction(cancelAction)
let editAction = UIAlertAction(title: "Edit",
style: UIAlertActionStyle.default,
handler: {(action) in
self.displayAddEditurlSearchAlert(isNew: false, index: row)})
alertController.addAction(editAction)
let shareAction = UIAlertAction(title: "Share",
style: UIAlertActionStyle.default,
handler: {(action) in self.shareurlSearch(index: row)})
alertController.addAction(shareAction)
present(alertController, animated: true,
completion: nil)
}
// displays add/edit dialog
func displayAddEditurlSearchAlert(isNew: Bool, index: Int?){
// create UIAlertController for user input
let alertController = UIAlertController(
title: isNew ? "Add URL" : "Edit URL",
message: isNew ? "" : "Modify your URL",
preferredStyle: UIAlertControllerStyle.alert)
// create UITextFields in which user can enter a new URL
alertController.addTextField(
configurationHandler: {(textField) in
if isNew {
textField.placeholder = "Enter URL"
}else{
textField.text = self.model.queryForTagAtIndex(index: index!)
}
})
alertController.addTextField(
configurationHandler: {(textField) in
if isNew {
textField.placeholder = "Tag your URL"
}else {
textField.text = self.model.tagAtIndex(index: index!)
textField.isEnabled = false
textField.textColor = UIColor.lightGray
}
})
// create Cancel action
let cancelAction = UIAlertAction(title: "Cancel",
style: UIAlertActionStyle.cancel, handler: nil)
alertController.addAction(cancelAction)
let saveAction = UIAlertAction(title: "Save",
style: UIAlertActionStyle.default,
handler: {(action) in
let query =
((alertController.textFields?[0])! as UITextField).text
let tag =
((alertController.textFields?[1])! as UITextField).text
// ensure query and tag are not empty
if !query!.isEmpty && !tag!.isEmpty {
self.model.saveQuery(
query: query!, forTag: tag!, syncToCloud: true)
if isNew {
let indexPath =
IndexPath(row: 0, section: 0)
self.tableView.insertRows(at: [indexPath],
with: .automatic)
}
}
})
alertController.addAction(saveAction)
present(alertController, animated: true,
completion: nil)
}
// displays share sheet
func shareurlSearch(index: Int) {
let message = "Check out this cool Web site"
let urlString = urlEncodeString(string: model.queryForTagAtIndex(index: index)!)
let itemsToShare = [message, urlString]
// create UIActivityViewController so user can share url
let activityViewController = UIActivityViewController(
activityItems: itemsToShare, applicationActivities: nil)
present(activityViewController,
animated: true, completion: nil)
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
// get query String
let query = String(model.queryForTagAtIndex(index: indexPath.row)!)
// create NSURL to perfrom url search
controller.detailItem = NSURL(string: urlEncodeString(string: query!))
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
// returns a URL encoded version of the query String
func urlEncodeString(string: String) -> String {
return string.addingPercentEncoding(
withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!
}
// MARK: - Table View
// callback that returns total number of sections in UITableView
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// callback that returns number of rows in the UITableView
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count
}
// callback that returns a configured cell for the given NSIndexPath
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// get cell
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
// set up long press guesture recognier
cell.textLabel!.text = model.tagAtIndex(index: indexPath.row)
// set up long press guesture recognizer
let longPressGestureRecognizer = UILongPressGestureRecognizer(
target: self, action: Selector(("tableViewCellLongPressed:")))
longPressGestureRecognizer.minimumPressDuration = 0.5
cell.addGestureRecognizer(longPressGestureRecognizer)
return cell
}
//callback that return whether a cell is editable
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true // all cells are editable
}
// callback that deletes a row from the UITableView
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
model.deleteurlSearchAtIndex(index: indexPath.row)
// remove UITableView row
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
// callback that returns whether cells can be moved
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
// callback that reorders keys when user moves them in the table
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath) {
// tell model to reorder tags based on UITableView order
model.moveTagAtIndex(oldIndex: sourceIndexPath.row,
toDestinationIndex: destinationIndexPath.row)
}
}
问题在于这个表达式:
Selector(("addButtonPressed:"))
替换为:
#selector(addButtonPressed)
请尝试清理生成文件夹并重新启动Xcode我不确定您是否需要DetailViewController.swift或Model.swift文件,但如果需要,我可以提供它们。请检查情节提要中的按钮,并查看addButtonPressed链接是否存在,以及该确切名称。在“无法识别的选择器”上进行搜索。这是非常常见的问题,在这里已经被很好地讨论过了。我投票将这个问题作为离题题结束,因为它是所有其他“未识别的选择器”问题的重复。例如,请参见我在这里的回答:这完全相同。您没有完全按照我说的(或您声称的)进行替换。如果你按照我说的做了,那么这行代码现在会变成
let addButton=UIBarButtonItem(barbuttonsystem:.add,target:self,action:#selector(addButtonPressed))
。显然不是。当我将代码更改为这个时,模拟器将不会加载。它只是停止并将我带到这里:->0x10af51dcc:movq-0x88(%rbp),%rdi并显示线程1:断点2.1。它不会给我任何其他错误,也不会像以前那样说明终止。这是因为您创建了一个断点,现在我们正在暂停它。这与这个问题无关。对不起,我对这个问题一无所知。谢谢你指出这一点。我花了点时间才弄明白那些是什么。这似乎现在起作用了。