假设我有一个由import UIKit
class TableViewController: UITableViewController {
var data = [String]()
@IBOutlet weak var editButton: UIBarButtonItem!
@IBAction func editAction(_ sender: UIBarButtonItem) {
self.tableView.isEditing = !self.tableView.isEditing
switch tableView.isEditing {
case true:
editButton.title = "Done"
default:
editButton.title = "Edit"
}
}
override func viewDidLoad() {
super.viewDidLoad()
data = ["Phone", "Pen", "Pencil"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return data.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! TitleTableViewCell
cell.titleLabel.text = data[section]
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete
{
data.remove(at: indexPath.row)
tableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let movedObjTemp = data[sourceIndexPath.row]
data.remove(at: sourceIndexPath.row)
data.insert(movedObjTemp, at: destinationIndexPath.row)
}
}
现在我想运行一个命令并通过返回的退出代码增加counter的值。
我想用自然语言counter=0
我正在尝试像counter = $counter + $?
这样的东西,但没有成功。在一行中完成这项工作的正确方法是什么?
答案 0 :(得分:1)
我建议用bash:
counter=$(($counter + $?))
这也是可能的:
counter=$((counter + $?))
或者:
declare -i counter=0 # set integer attribute
<your command>
counter=counter+$?
或者:
declare -i counter=0
<your command>
counter=+$?
答案 1 :(得分:1)
您可以将$?
的值分配给中间变量,然后使用算术上下文添加:
将$?
设为22:
$ awk 'BEGIN {exit 22}'
$ rtr=$?
$ counter=1
$ echo $((counter+rtr))
23
答案 2 :(得分:0)
在UNIX中,您可以尝试:
计数器= 1;
计数器= expr $counter + 1
;
echo $ counter;
注意:expr $counter + 1
在&#39; +&#39;