为什么这个简单的目录处理应用程序会失败?
Why is this simple Directory Handling app failing?
我正在关注 this tutorial 以尝试学习使用 iPhone.The 应用程序时的目录处理非常简单。当用户在文本字段中输入文本后触摸按钮时,该文本将保存到文件中。下次启动应用程序时,应用程序会读取文件的内容并将其预加载到文本字段中。我完全按照教程进行操作,但是当我 运行 应用程序时,出现错误:
2017-05-29 15:20:03.992119-0700 directoryHandling[293:23526] [Accessibility] ****************** Loading GAX Client Bundle ****************
2017-05-29 15:20:04.861072-0700 directoryHandling[293:23526] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<directoryHandling.ViewController 0x100b12700> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key saveText.'
*** First throw call stack:
(0x18b0f2fd8 0x189b54538 0x18b0f2ca0 0x18bb0763c 0x191586218 0x191729ff4 0x18b013f8c 0x1917289e8 0x1915891f0 0x19135353c 0x191221c44 0x191221b78 0x1912283f8 0x191225894 0x19f89f6dc 0x1912972fc 0x19149f8b4 0x1914a52a8 0x1914b9de0 0x1914a253c 0x18cc9b884 0x18cc9b6f0 0x18cc9baa0 0x18b0a1424 0x18b0a0d94 0x18b09e9a0 0x18afced94 0x19128c45c 0x191287130 0x100055864 0x189fdd59c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
这是我目前拥有的应用程序代码:
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var textBox: UITextField!
var fileMgr: FileManager = FileManager.default
var docsDir: String?
var dataFile: String?
override func viewDidLoad() {
super.viewDidLoad()
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory,
in: .userDomainMask)
dataFile =
dirPaths[0].appendingPathComponent("datafile.dat").path
if fileMgr.fileExists(atPath: dataFile!) {
let databuffer = fileMgr.contents(atPath: dataFile!)
let datastring = NSString(data: databuffer!,
encoding: String.Encoding.utf8.rawValue)
textBox.text = datastring as? String
}
}
@IBAction func saveText(_ sender: UIButton)
{
let databuffer =
(textBox.text)!.data(using: String.Encoding.utf8)
fileMgr.createFile(atPath: dataFile!, contents: databuffer,
attributes: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我不知道从这里到哪里去。非常感谢您的帮助!
编辑:
以下是我为按钮建立连接的方式:
这里是检查员:
问题是,您创建了一个 saveText
出口,从代码中删除了它,但您没有在界面生成器中断开它。
只需单击 "x" 图标断开连接检查器中的插座即可。
我正在关注 this tutorial 以尝试学习使用 iPhone.The 应用程序时的目录处理非常简单。当用户在文本字段中输入文本后触摸按钮时,该文本将保存到文件中。下次启动应用程序时,应用程序会读取文件的内容并将其预加载到文本字段中。我完全按照教程进行操作,但是当我 运行 应用程序时,出现错误:
2017-05-29 15:20:03.992119-0700 directoryHandling[293:23526] [Accessibility] ****************** Loading GAX Client Bundle ****************
2017-05-29 15:20:04.861072-0700 directoryHandling[293:23526] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<directoryHandling.ViewController 0x100b12700> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key saveText.'
*** First throw call stack:
(0x18b0f2fd8 0x189b54538 0x18b0f2ca0 0x18bb0763c 0x191586218 0x191729ff4 0x18b013f8c 0x1917289e8 0x1915891f0 0x19135353c 0x191221c44 0x191221b78 0x1912283f8 0x191225894 0x19f89f6dc 0x1912972fc 0x19149f8b4 0x1914a52a8 0x1914b9de0 0x1914a253c 0x18cc9b884 0x18cc9b6f0 0x18cc9baa0 0x18b0a1424 0x18b0a0d94 0x18b09e9a0 0x18afced94 0x19128c45c 0x191287130 0x100055864 0x189fdd59c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
这是我目前拥有的应用程序代码:
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var textBox: UITextField!
var fileMgr: FileManager = FileManager.default
var docsDir: String?
var dataFile: String?
override func viewDidLoad() {
super.viewDidLoad()
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory,
in: .userDomainMask)
dataFile =
dirPaths[0].appendingPathComponent("datafile.dat").path
if fileMgr.fileExists(atPath: dataFile!) {
let databuffer = fileMgr.contents(atPath: dataFile!)
let datastring = NSString(data: databuffer!,
encoding: String.Encoding.utf8.rawValue)
textBox.text = datastring as? String
}
}
@IBAction func saveText(_ sender: UIButton)
{
let databuffer =
(textBox.text)!.data(using: String.Encoding.utf8)
fileMgr.createFile(atPath: dataFile!, contents: databuffer,
attributes: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我不知道从这里到哪里去。非常感谢您的帮助!
编辑:
以下是我为按钮建立连接的方式:
这里是检查员:
问题是,您创建了一个 saveText
出口,从代码中删除了它,但您没有在界面生成器中断开它。
只需单击 "x" 图标断开连接检查器中的插座即可。