2017-01-06 250 views
4

我是Swift 3的新手,想創建一個小小的待辦事項列表。但在viewDidLoad應用程序總是崩潰,因爲UILongPressGestureRecognizer。我在互聯網上搜索,但我沒有找到工作解決方案。UILongPressGestureRecognizer in Swift 3

這裏是我的代碼,每次它說: 「主題1:1.1的斷點」 在符合UILongPressGestureRecognizer:

class ViewController: UIViewController, UITableViewDelegate { 


@IBOutlet weak var newButton: UIButton! 
@IBOutlet weak var tableView: UITableView! 
@IBOutlet weak var editButton: UIButton! 

var todoList = Todo.load(){ 
    didSet{ 
     Todo.save(todoList) 
    } 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.delegate = self 
    tableView.dataSource = self 

    let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongPress(_:))) 
    lpgr.minimumPressDuration = 1.2 
    tableView.addGestureRecognizer(lpgr) 
} 


func handleLongPress(_ gesture: UILongPressGestureRecognizer){ 
    if gesture.state != .began { return } 
    let pt = gesture.location(in: tableView) 
    let path = tableView.indexPathForRow(at: pt) 
    if let row = (path as NSIndexPath?)?.row, 
     let cell = tableView.cellForRow(at: path!){ 
    showPopup(sender: cell, mode: "edit", text: todoList[row], row: row) 
    } 
} 

下面是todo.txt文件的代碼:

struct Todo { 
static func save(_ data: [String]){ 
    if let url = docUrl(for: "todo.txt"){ 
     do { 
      let str = data.joined(separator: "\n") 
      try str.write(to: url, atomically: true, encoding: .utf8) 
     } catch { 
      print(error) 
     } 
    } 
} 
static func load() -> [String] { 
    if let url = docUrl(for: "todo.txt"){ 
     do{ 
      let str = try String(contentsOf: url, 
           encoding: .utf8) 
      return str.characters 
       .split {$0 == "\n"} 
       .map { String($0)} 
     } catch { 
      print(error) 
     } 
    } 
    return [] 
} 
private static func docUrl(for filename: String) -> URL? { 
    let urls = FileManager.default.urls(for: .documentDirectory, 
             in: .userDomainMask) 
    if let docDir = urls.first { 
     return docDir.appendingPathComponent(filename) 
    } 
    return nil 
} 

}

這裏是我的錯誤報導:

Error Domain=NSCocoaErrorDomain Code=260 "The file 「todo.txt」 couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/mkartds/Library/Developer/CoreSimulator/Devices/5D70E1CB-6D29-49E4-BCD1-316B5022F085/data/Containers/Data/Application/34869E75-E498-4674-B504-E7867935E3FE/Documents/todo.txt, NSUnderlyingError=0x61000004a830 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 

(lldb)

我該怎麼辦?

+0

請分享錯誤日誌跟蹤,我在這方面的工作,也許問題是其他地方。 –

+0

**線程1:斷點1.1 **意味着您意外激活了斷點,只需刪除或禁用斷點即可。 –

+0

我將錯誤報告添加到了我的問題中!但我現在可以解決這個問題! – Jan

回答

0

假設您已在項目目標中添加了todo.txt文件。

嘗試更新docUrl方法具有以下,

private static func docUrl() -> URL? { 
    let bundle = Bundle.main 
    let path = bundle.path(forResource: "todo", ofType: "txt") 
    let fileURL = URL(fileURLWithPath: path) 
    return fileURL 
} 

停用斷點:

enter image description here

+0

即使刪除了UILongPressGestureRecognizer部件,我也得到相同的斷點,但沒有任何錯誤!只是:「(lldb)」 – Jan

+0

@Jan,請停用您的斷點,請參閱上圖。 –

+0

哦,工作!非常感謝你! – Jan