LoginSignup
4
4

More than 5 years have passed since last update.

Swift (iOS)で日本語変数名が便利だなと思った例

Posted at

Swiftは日本語変数名が使える!

Swiftを発表したKeynoteでも発表されていましたように、マルチバイトの文字列がSwiftではどこでもつかえるようになっています。漢字や絵文字まで使い放題です。

ただ、いつ、どういう場面で使えるのかが疑問。
PHPerな私はCakePHP with PHPUnitのテストケースの名前をpublic function test入力値は200字以内() {...} のようにしたことはありますが、Swiftでどんな場合に有用なのか、まとめることにしました。

UIAlertActionの名前

SampleViewController.swift

var alertController = UIAlertController(title: "あなたはシカオさんではありませんね?", message:"", preferredStyle: UIAlertControllerStyle.Alert)

let はいAlertAction = UIAlertAction(title: "はい", style: UIAlertActionStyle.Default, handler: {...()...})
let いいえAlertAction = UIAlertAction(title: "いいえ", style: UIAlertActionStyle.Cancel, handler: nil)

alertController.addAction(はいAlertAction)
alertController.addAction(いいえAlertAction)
self.presentViewController(alertController, animated: true, completion:nil)

「はい」をおした時のAlertActionだからはいAlertAction。「いいえ」をおした時のAlertActionだからいいえAlertAction。わかりやすいですね。

もしこれがyesAlertActionnoAlertActionだと、この質問の仕方だとどっちがはいを押したときのアクションなのか、書き手の意図がややわかりづらくなります。

(以後追加していきたいと思います)

他にもマルチバイト変数名が有用な場面を発見したら追加していきます。募集中です。

4
4
0

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
4
4