[Swift] Determine the constellation from the date of birth entered in the UIDatePicker

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.

Completed image of the constellation judgment app

horoscope.GIF

1. Get the value of UIDatePicker

Since there is an Action that is triggered when the value of UIDatePicker is changed, use this to get the value of DatePicker. スクリーンショット 2021-01-04 14.31.57.png

2. Change the value obtained by valueChangedAction from Date type to Int type

@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)

3. Return the corresponding constellation from the birthday (birthDayInt)

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"
        }
    }

Substitute the returned constellations for images and text


//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
    }

Whole code

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"
        }
    }
}

reference

Swift Constellation Judgment <a href=""https://www.irasutoya.com/search?q=%E6%98%9F%E5%BA%A7> Bachelor of Arts (image)

Summary

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

[Swift] Determine the constellation from the date of birth entered in the UIDatePicker
[Swift] Change the textColor of UIDatePicker
In Time.strptime,% j (total date of the year) is
Find the approximate value of log (1 + x) in Swift
Determine that the value is a multiple of 〇 in Ruby
The story of throwing BLOB data from EXCEL in DBUnit
[Swift] The color of the Navigation Bar is different (lighter) from the specified color.
The basic basis of Swift dialogs
Order of processing in the program
[Payment of work] Summarize the mistakes made in 2020 [2nd year from inexperience]
The story of acquiring Java Silver in two months from completely inexperienced.
Confirmation and refactoring of the flow from request to controller in [httpclient]
This and that of the implementation of date judgment within the period in Java
Find the maximum and minimum of the five numbers you entered in Java