月曜が来た
あたらーしー朝がきたー!
げつよーのあーさーだ!
みんなー!!!朝だよー!起きてー!!カンカンカンカンカンカン!!!カンカンカン!!!!!!!起きてー!!
おはようございます。僕です。先日Swiftと呼ばれる呪文をいじったので、早速実践に取り入れようといじっていたら月曜になっていました。どうしてこうなってしまったのか。どうして僕はこんなことをしているのか……。
Swiftをさわってきた
Xcode触らなすぎてNSUnknownKeyException
とか言うコードの書き方と関係ないエラーの対応とかで時間かかってしまった。
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key xxx.'
対処法は、対象のパーツを右クリして不要な「Referencing Outlets」を外すでよろしいそうです。
そんで月曜なので、いつもの曜日判定機を作りました。月曜のときに右のやつが表示されます。
以下ソース。
import UIKit class ViewController: UIViewController { @IBOutlet weak var MondayText: UILabel! override func viewDidLoad() { super.viewDidLoad() let dateFormater = DateFormatter() dateFormater.locale = Locale(identifier: "ja_JP") dateFormater.dateFormat = "EEEEE" let date = dateFormater.string(from: Date()) if date == "月"{ MondayText.text = date+"曜日\n恐怖の1日が始まる" view.backgroundColor = UIColor.red MondayText.textColor = UIColor.white MondayText.sizeToFit() MondayText.font = UIFont.systemFont(ofSize: 25) }else{ MondayText.text = "今日も一日がんばるぞい" } } }
DateFormatter
を使えば日付操作がかなり楽になるってのがわかった。Swiftでぐぐってもスズキのやつが検索上位にくるので無駄にヘイトが溜まる。Swift4になっても情報少ないねーこれ……。
参考:標準機能ドキュメント
Foundation | Apple Developer Documentation