数组类型现在用元素类型两边的方括号书写
Array types are now written with the brackets around the element type
这是我的代码 (TaskManager.swift):
import UIKit
var taskMgr: TaskManager = TaskManager ()
struct task {
var name = "untitled"
var desc = "none"
}
class TaskManager: NSObject {
var tasks = task[]()
func addTask (name: String, desc: String) {
tasks.append (task (name: name, desc: desc))
}
}
然后我得到了这个
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:11:21: Array types are now written with the brackets around the element type
它说
Fix-it: Insert "["
我点击了它,然后那一行(第 11 行)变成了这个:
var tasks = [task[]()
我遇到了这些错误
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:11:26: Expected ']' in container literal expression
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:11:26: Expected ',' separator
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:13:5: Expected expression in container literal
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:14:15: Cannot invoke 'append' with an argument list of type '(task)'
感谢您的任何建议!
这是 FirstViewController.swift 中的另外 2 个错误:
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/FirstViewController.swift:21:14: 'text' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/FirstViewController.swift:22:30: Cannot assign to 'detailTextLabel' in 'cell'
这是代码
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskMgr.tasks.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell (style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
cell.textLabel = ""
cell.detailTextLabel = ""
//cell.text = taskMgr.tasks[indexPath.row].name
//cell.detailTextLabel = taskMgr.tasks[indexPath.row].desc
return cell
}
}
看起来 Fix-it 搞砸了。 Swift 在类型周围使用方括号。正确的版本是这样的:
var tasks = [task]()
这是shorthand的写法;有一个更长的版本:
var tasks = Array<task>()
但首选 shorthand 版本。
您的语法不正确。
任务数组可以这样初始化:-
Var tasks = [Task]()
然后你可以使用附加方法在其中添加"Task"对象。
tasks.append(your Task object)
这是我的代码 (TaskManager.swift):
import UIKit
var taskMgr: TaskManager = TaskManager ()
struct task {
var name = "untitled"
var desc = "none"
}
class TaskManager: NSObject {
var tasks = task[]()
func addTask (name: String, desc: String) {
tasks.append (task (name: name, desc: desc))
}
}
然后我得到了这个
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:11:21: Array types are now written with the brackets around the element type
它说
Fix-it: Insert "["
我点击了它,然后那一行(第 11 行)变成了这个:
var tasks = [task[]()
我遇到了这些错误
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:11:26: Expected ']' in container literal expression /Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:11:26: Expected ',' separator /Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:13:5: Expected expression in container literal /Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/TaskManager.swift:14:15: Cannot invoke 'append' with an argument list of type '(task)'
感谢您的任何建议!
这是 FirstViewController.swift 中的另外 2 个错误:
/Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/FirstViewController.swift:21:14: 'text' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift /Users/David/Documents/360Drive/Xcode/Try/RandomTries/ToDoList/ToDoList/FirstViewController.swift:22:30: Cannot assign to 'detailTextLabel' in 'cell'
这是代码
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskMgr.tasks.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell (style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
cell.textLabel = ""
cell.detailTextLabel = ""
//cell.text = taskMgr.tasks[indexPath.row].name
//cell.detailTextLabel = taskMgr.tasks[indexPath.row].desc
return cell
}
}
看起来 Fix-it 搞砸了。 Swift 在类型周围使用方括号。正确的版本是这样的:
var tasks = [task]()
这是shorthand的写法;有一个更长的版本:
var tasks = Array<task>()
但首选 shorthand 版本。
您的语法不正确。 任务数组可以这样初始化:-
Var tasks = [Task]()
然后你可以使用附加方法在其中添加"Task"对象。
tasks.append(your Task object)