【问题标题】:UILongPressGestureRecognizer in Swift 3Swift 3 中的 UILongPressGestureRecognizer
【发布时间】:2017-01-06 10:22:24
【问题描述】:

我是 Swift 3 的新手,想创建一个小待办事项列表。但是在 viewDidLoad 中,由于 UILongPressGestureRecognizer,App 总是崩溃。我在互联网上搜索,但没有找到有效的解决方案。

这是我的代码,每次它在 UILongPressGestureRecognizer 行中显示“线程 1:断点 1.1”:

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)

我该怎么办?

【问题讨论】:

  • 请分享错误日志跟踪,我有这个工作,也许问题出在其他地方。
  • 线程 1:断点 1.1 表示你不小心激活了断点,只需删除或禁用它即可。
  • 我将错误报告添加到我的问题中!但我现在可以解决这个问题!
  • @Jan 这意味着 LongPress 手势没有错误,错误它自己说文件 todo.txt 不存在并且您正在尝试修改它,这是不可能的。
  • 我刚刚添加了文件,但是当我删除 UILongPressGestureRecognizer 时,viewDidLoad 中有一个断点。在一个空行中。

标签: ios swift uilongpressgesturerecogni


【解决方案1】:

假设您在项目的目标中添加了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
}

停用断点:

【讨论】:

  • 我得到了相同的断点,即使我删除了 UILongPressGestureRecognizer 部分,但没有任何错误!只是:“(lldb)”
猜你喜欢
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多