LoginSignup
1
2

More than 5 years have passed since last update.

Swift3.0以降とその前の様々な記述の変更について。

Last updated at Posted at 2018-01-09

Swift3.0~と以前とでは大きな違いがあるみたい。

【Xcode / Swift入門】簡単なToDoリストアプリを作ってみよう

上記を参考にToDoアプリを作ろうと思っていたところ、どうやらswift3.0より前のものだったようで、いくつか記述方法が変更されていたのでそれらをメモ代わりにまとめました。
(今更感もあるのだが、気にしない。)

当方、swiftに触れたのもほぼ初めてのところもありますので、ご指摘等ありましたらなんなりと仰って下さい。

※ まだ正常な動きが出来ていません。m(_ _;)m

NSUserdefaults

変更前

以下引用。

次に、ボタンをクリックされた際にNSUserDefaultsにテキストフィールドの値を保存するよう記述します。テキストフィールドの値をString型の配列todoItemに格納し、NSUserDefaultsにSetします。合わせて、テキストフィールド内の文字列は、空欄にします。

@IBAction func addItem(sender: AnyObject) {
        todoItem.append(itemText.text!)
        itemText.text = ""
        NSUserDefaults.standardUserDefaults().setObject(todoItem, forKey: "todoList")
}

変更後

@IBAction func addItem(_ sender: AnyObject) {
    todoItem.append(itemText.text!)
    itemText.text = ""
    UserDefaults.standard.set(todoItem, forKey: "todoList")
// ↑NSUserDefaults以下が変更された。
}

touchesBeganメソッド

変更前

次に、キーボード以外をタッチするとキーボードが下がる機能です。class AddToDo: UIViewController 内に以下を記述します。これは、決まり文句みたいなものです。

override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}

変更後

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

補足

シミュレーターでキーボードが表示されなくなった。
それについては以下で解決。
iOS8シミュレータでキーボードが表示されなくなった場合の対処方法
これじゃない可能性もあるけど。


resignFirstResponder()メソッド

変更前

最後に、キーボードのreturnを押下すると、キーボードが下がる機能です。class AddToDo: UIViewController 内に以下を記述します。こちらも決まり文句みたいなものです。

func textFieldShouldReturn(textField: UITextField!) -> Bool {        
    itemText.resignFirstResponder()        
    return true        
}

変更後 うまくいかないので調査中。

func textFieldShouldReturn(_ textField: UITextField!) -> Bool {
    // キーボードを閉じる
    itemText.resignFirstResponder()
    return true
}

viewDidLoad()

変更前

次に、NSUserDefaultsの値を取得し、先ほどのAddToDo.swiftにて作成した配列型の変数todoItemに代入します。これは起動時に呼ばれる関数であるviewDidLoad()内に記述します。

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    if NSUserDefaults.standardUserDefaults().objectForKey("todoList") != nil {
        todoItem = NSUserDefaults.standardUserDefaults().objectForKey("todoList") as! [String]         
    }
}

変更後

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    if UserDefaults.standard.object(forKey:"todoList") != nil {
        todoItem = UserDefaults.standard.object(forKey:"todoList") as! [String]
    }
}

UITableViewCell

変更前

2つ目は各行へ値を表示する関数です。NSUserDefaultsの値が代入されている変数todoItemが保持している文字列を各テーブルに表示していきます。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellValue = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    cellValue .textLabel?.text = todoItem[indexPath.row]
    return cellValue
}

変更後

// Default -> default に変更
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellValue = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    cellValue .textLabel?.text = todoItem[indexPath.row]
    return cellValue
}

UITableViewCellEditingStyle.Delete

変更前

次に、下記の関数を記述します。これはTableViewのcellをEdit(編集)したい場合に使用する関数です。今回は削除(delete)をするため、削除する場合は、関数内で UITableViewCellEditingStyle.Deleteを使用します。
また、配列から該当の値を削除するため、removeAtIndex()関数も利用します。削除後は、再度NSUserDefaultsに値をセットし、最後にTableView全体を更新します。TableViewの更新には、先ほど作成したTableViewの変数にreloadData()と記述します。

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
    if editingStyle == UITableViewCellEditingStyle.Delete{
        todoItem.removeAtIndex(indexPath.row)
        NSUserDefaults.standardUserDefaults().setObject(todoItem, forKey: "todoList") 
        todolistTable.reloadData()     
    }
}

変更後

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
    if editingStyle == UITableViewCellEditingStyle.delete{
        todoItem.remove(at: indexPath.row)
        UserDefaults.standard.set(todoItem, forKey: "todoList")
        todolistTable.reloadData()
    }
}

viewDidAppear

変更前

最後に、テーブル(リスト)を下に引っ張って更新機能を記述します。
これは決まり文句のようなもので、下記を記述します。

override func viewDidAppear(animated: Bool) {
    todolistTable.reloadData()
}

変更後

override func viewDidAppear(_ animated: Bool) {
    todolistTable.reloadData()
}

最後に

結局のところ、表示はされているのですが、AddしたテキストがListページに出てこないので追記で修正していきます。

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2