gpt4 book ai didi

ios - 数组类型现在用元素类型两边的方括号书写

转载 作者:搜寻专家 更新时间:2023-11-01 06:46:02 24 4
gpt4 key购买 nike

这是我的代码(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
}

}

最佳答案

看起来修复搞砸了。 Swift 在类型周围使用方括号。正确的版本是这样的:

var tasks = [task]()

这是速记符号;有一个更长的版本:

var tasks = Array<task>()

但首选简写版本。

关于ios - 数组类型现在用元素类型两边的方括号书写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30825750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com