For learning UIDatePicker, I created an application that determines the constellation from the date of birth and displays the target constellation with an image and a label.
Since there is an Action that is triggered when the value of UIDatePicker is changed, use this to get the value of DatePicker.
@IBAction func valueChangedAction(_ sender: UIDatePicker) {
//Since the year is not required, only the dateFormat is acquired.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMdd"
//Convert Date obtained from DatePicker to String type with DateFormatter
let birthdayString = dateFormatter.string(from: sender.date)
//Convert String type birthday to Int type
let birthdayInt = Int(birthdayString)
Set the range operator in the switch condition and return the corresponding constellation
private func seizaCheck(withBirthday birtyday: Int) -> String {
switch birtyday {
case 321...419:
return "Aries"
case 420...520:
return "Taurus"
case 521...621:
return "Gemini"
case 622...722:
return "Cancer"
case 723...822:
return "Leo"
case 823...922:
return "Virgo"
case 923...1023:
return "Libra"
case 1024...1122:
return "Scorpion"
case 1123...1221:
return "Sagittarius"
case 120...218:
return "Aquarius"
case 219...320:
return "Pisces"
default:
return "Capricorn"
}
}
//Optional binding of optional birthdayInt
if let birthdayInt = birthdayInt {
//Birthday (birtdayInt) as an argument of the constellation check function)Get the constellation returned by substituting
let seiza = seizaCheck(withBirthday: birthdayInt)
//UIImage of the character string acquired by the constellation check function(named:)And seiza Label.Assign to text
seizaImageView.image = UIImage(named: seiza)
seizaLabel.text = seiza
}
class HoroscopeViewController: UIViewController {
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var seizaImageView: UIImageView!
@IBOutlet weak var seizaLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Change the text color of datePicker to white
datePicker.setValue(UIColor.white, forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")
}
@IBAction func ChangedDatePicker(_ sender: UIDatePicker) {
//Since the year is not required, only the dateFormat is acquired.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMdd"
//Convert Date obtained from DatePicker to String type with DateFormatter
let birthdayString = dateFormatter.string(from: sender.date)
//Convert to Int type because the initial 0 such as 0215 is not required.
let birthdayInt = Int(birthdayString)
//Optional binding of optional birthdayInt
if let birthdayInt = birthdayInt {
//Birthday (birtdayInt) as an argument of the constellation check function)Get the constellation returned by substituting
let seiza = seizaCheck(withBirthday: birthdayInt)
//UIImage of the character string acquired by the constellation check function(named:)And seiza Label.Assign to text
seizaImageView.image = UIImage(named: seiza)
seizaLabel.text = seiza
}
}
private func seizaCheck(withBirthday birtyday: Int) -> String {
switch birtyday {
case 321...419:
return "Aries"
case 420...520:
return "Taurus"
case 521...621:
return "Gemini"
case 622...722:
return "Cancer"
case 723...822:
return "Leo"
case 823...922:
return "Virgo"
case 923...1023:
return "Libra"
case 1024...1122:
return "Scorpion"
case 1123...1221:
return "Sagittarius"
case 120...218:
return "Aquarius"
case 219...320:
return "Pisces"
default:
return "Capricorn"
}
}
}
Swift Constellation Judgment <a href=""https://www.irasutoya.com/search?q=%E6%98%9F%E5%BA%A7> Bachelor of Arts (image)
I want to be able to use the switch statement better than the if statement because it can be described smarter depending on how it is used.
Recommended Posts