Swift 2.0ã§å°å
¥ããã
ã¬ãŒãã¹ããŒãã¡ã³ãã¯ãæ¡ä»¶ãæºããããªãå Žåã«çµäºãæäŸããŸãã 以äžã«äŸã瀺ããŸãã
guard dividend != nil else { return nil }
ãªãã·ã§ã³ã®ãã€ã³ãã£ã³ã°ã«ã¬ãŒãã¹ããŒãã¡ã³ãã䜿çšããããšãã§ã
ãŸã ããã®åŸãå±éãããå€æ°ã¯
ã¬ãŒãã¹ããŒãã¡ã³ãã®å€åŽã§å©çšå¯èœã«ãªããŸãã
guard let dividend = dividend else { return .none }
ãããã£ãŠã次ã®ããã«é¢æ°ãæžãæããããšãã§ããŸãã
func divide(_ dividend: Double?, by divisor: Double?) -> Double? { guard let dividend = dividend else { return nil } guard let divisor = divisor else { return nil } guard divisor != 0 else { return nil } return dividend / divisor }
被é€æ°ãšé€æ°ã¯æ¢ã«ã¢ã³ããã¯ãããŠããããããããªãã·ã§ã³ã§ã¯ãªãäžå€å€æ°ã«å
¥ããŠããããã匷å¶çãªã¢ã³ããã¯ããªãããšã«æ³šæããŠãã ããã
ãŸããguardã¹ããŒãã¡ã³ãå
ã®ã¢ã³ããã¯ããããªãã·ã§ã³ã®çµæã¯ãguardã¹ããŒãã¡ã³ãå€ã§ãå©çšã§ããããšã«æ³šæããŠãã ããã
ã¬ãŒãã¹ããŒãã¡ã³ããã°ã«ãŒãåããããšã«ãããé¢æ°ãããã«ç°¡çŽ åã§ããŸãã
func divide(_ dividend: Double?, by divisor: Double?) -> Double? { guard let dividend = dividend, let divisor = divisor, divisor != 0 else { return nil } return dividend / divisor }