'contentsOf ' 在我使用数据时未显示
'contentsOf ' is not showing up when I use Data
如果让数据=试试?数据(内容:数据文件路径!)
就像我上次做的那样,我试着再做一次,当我输入数据时,这个“contentsOf”没有出现,如果我手动输入,会弹出一个错误,说找不到 contentsOf: in scope
是否有我可以使用 contentsOf: 的特定条件?
import UIKit
class TodoListViewController: UITableViewController {
var itemArray: [Data] = []
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("item.plist")
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TodoListCell", for: indexPath)
cell.textLabel?.text = itemArray[indexPath.row].title
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
itemArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add Todo List", message: nil, preferredStyle: UIAlertController.Style.alert)
alert.addTextField { (myTextField) in
myTextField.placeholder = "What's your new plan?"
textField = myTextField
}
let action = UIAlertAction(title: "Add item", style: UIAlertAction.Style.default) { (alertAction) in
let newItem = Data()
newItem.title = textField.text!
self.itemArray.append(newItem)
self.saveData()
self.tableView.reloadData()
}
let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)
alert.addAction(action)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
}
func saveData() {
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(itemArray)
try data.write(to: path)
} catch {
print(error)
}
}
func loadData() {
let data = **Data(contentsOf:)**
}
}
鉴于
let newItem = Data()
newItem.title = textField.text!
似乎没有在此处抛出错误让我相信您创建了自己的 class/struct,名称为 Data
在这种情况下,可能未定义和找到 Data(contentsOf: ...)
如果我是对的,有两个选择:
- 尝试Foundation.Data(contentsOf: ...)
- 重命名您的数据声明
https://developer.apple.com/documentation/foundation/data
恕我直言,您应该将数据重命名为 class,因为它已经在 Apple Foundation 中声明了
如果让数据=试试?数据(内容:数据文件路径!)
就像我上次做的那样,我试着再做一次,当我输入数据时,这个“contentsOf”没有出现,如果我手动输入,会弹出一个错误,说找不到 contentsOf: in scope
是否有我可以使用 contentsOf: 的特定条件?
import UIKit
class TodoListViewController: UITableViewController {
var itemArray: [Data] = []
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("item.plist")
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TodoListCell", for: indexPath)
cell.textLabel?.text = itemArray[indexPath.row].title
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
itemArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add Todo List", message: nil, preferredStyle: UIAlertController.Style.alert)
alert.addTextField { (myTextField) in
myTextField.placeholder = "What's your new plan?"
textField = myTextField
}
let action = UIAlertAction(title: "Add item", style: UIAlertAction.Style.default) { (alertAction) in
let newItem = Data()
newItem.title = textField.text!
self.itemArray.append(newItem)
self.saveData()
self.tableView.reloadData()
}
let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)
alert.addAction(action)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
}
func saveData() {
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(itemArray)
try data.write(to: path)
} catch {
print(error)
}
}
func loadData() {
let data = **Data(contentsOf:)**
}
}
鉴于
let newItem = Data()
newItem.title = textField.text!
似乎没有在此处抛出错误让我相信您创建了自己的 class/struct,名称为 Data
在这种情况下,可能未定义和找到 Data(contentsOf: ...)
如果我是对的,有两个选择:
- 尝试Foundation.Data(contentsOf: ...)
- 重命名您的数据声明
https://developer.apple.com/documentation/foundation/data
恕我直言,您应该将数据重命名为 class,因为它已经在 Apple Foundation 中声明了