Swift初心者です。簡単なアラームアプリを作りたいと思っています。
現在ViewControllerにインスタンスを生成し、コードを書いています。
現在やりたいことは、
①MainStoryBoardに設置したUISwitch Buttonをオンにすると、クラスに定義した「vibration」のBool型にon/offと、コンソール上に出力させる
②同様にMainStoryBoard上のUIDatePickerで日にち・時間を選択すると、クラスに定義した「time」のDate型に、yyyy/MM/dd HH:mmのフォーマットで、コンソール上に出力させる
です。
今のところここまで進みました。
エラーが出てしまいRun出来ない状態で、途方に暮れております。
また、②のDatePickerで時間を表示させるやり方が分からず、苦戦しております。
稚拙な説明で分かりづらいかと思いますが、どうかご教示頂けると幸いです。
よろしくお願い致します。
追記:
コメントありがとうございます。
こちらがクラスを定義したコードで、
swift
1import Foundation 2//Alarm型の定義 3class Alarm { 4 var title: String? 5 var time: Date? 6 var duration: Int? 7 var musicURL: String? 8 var vibration: Bool? 9 } 10 11func initialAlarm() -> Alarm { 12 return Alarm() 13}
こちらが現在コードを書いているViewControllerです。
swift
1// 2// AlarmCreateViewController.swift 3// share_alarm 4// 5// Created by -- on 2016-12-11. 6// Copyright © 2016 --. All rights reserved. 7// 8 9import Foundation 10import UIKit 11 12class AlarmCreateViewController: UIViewController { 13 var alarm: Alarm = initialAlarm() 14 15 //Durationを入力 16 //When the value of Int is written within UITextField named sender, the values are inserted into the variants "duration" in the class "Alarm". 17 @IBAction func input(_ sender: UITextField) { 18 if let text = sender.text { 19 if let duration = Int(text){ 20 alarm.duration = duration 21 } 22 } 23 dump(alarm) 24 } 25 26 //Titleを入力 27 //When the value of String is written within UITextField named sender, the values are inserted into the variants "title" in the class "Alarm". 28 @IBAction func titleText(_ sender: UITextField) { 29 if let text2 = sender.text { 30 if let title = String(text2){ 31 alarm.title = title 32 } 33 dump(alarm) 34 } 35 } 36 37 //vibrationをオン・オフにする 38 //When the UISwitch is on, the state of vibration "on" is inserted into the variants "vibration" in the class "Alarm". 39 @IBAction func onOffSwitch(_ sender: UISwitch) { 40 let buttonState: Bool = sender.isOn 41 if sender.isOn { 42 let vibrationState = Bool(true) 43 alarm.vibration = vibrationState 44 dump(alarm) 45 }else{ 46 let vibrationState = Bool(false) 47 alarm.vibration = vibrationState 48 dump(alarm) 49 } 50 } 51 52 53 54 override func viewDidLoad() { 55 super.viewDidLoad() 56 // Do any additional setup after loading the view, typically from a nib. 57 } 58 59 override func didReceiveMemoryWarning() { 60 super.didReceiveMemoryWarning() 61 // Dispose of any resources that can be recreated. 62} 63}
エラーは、
swift
1 let buttonState: Bool = sender.isOn
で出ております。
行き詰まっており、どうしたらよいのか分からない状態で、、お手数ですが、どうかよろしくお願い致します。
ーーーーーー
追記2 新たなエラー
Swift
1 //vibrationをオン・オフにする 2 //When the UISwitch is on, the state of vibration "on" is inserted into the variants "vibration" in the class "Alarm". 3 @IBAction func onOffSwitch(_ sender: UISwitch) { 4 var buttonState: Bool = sender.isOn 5 if buttonState == true{ 6 var vibrationState = Bool(on) 7 alarm.vibration = vibrationState 8 dump(alarm) 9 }else{ 10 let vibrationState = Bool(off) 11 alarm.vibration = vibrationState 12 dump(alarm) 13 } 14 }
エラーは、
swift
1var vibrationState = Bool(on) 2と 3var vibrationState = Bool(off) 4
に出ております。
回答1件
あなたの回答
tips
プレビュー