2017-10-15 103 views
-3

我想調用if語句中的函數,但總是收到錯誤消息「無法轉換類型'Int.Type'的值預期參數類型「內部」。我在做什麼錯?無法將'Int.Type'類型的值轉換爲預期的參數類型'Int'

func isLeapYear(year: Int) -> Bool { 

    if year % 4 != 0{ 
     return false 
    } 
    else if year % 100 != 0{ 
     return true 
    } 
    else if year % 400 != 0{ 
     return false 
    } 
    else{ 
     return true 
    } 
} 

func nextDay(year: Int, month: Int, day: Int) -> (year: Int, month: Int, day: Int) { 

    if isLeapYear(year: Int) == true { 
     if day < daysOfMonths_leap[month-1] { 
      return (year, month, day + 1) 
     }else { 
      if month == 12 { 
       return (year + 1, 1, 1) 
      } else { 
       return (year, month + 1, 1) 
      } 
     } 
    } 
} 
+2

您可能需要閱讀[ 「定義和調用功能」(https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions .html#// apple_ref/doc/uid/TP40014097-CH10-ID159)再次...'isLeapYear(year:Int)'是*不*你如何調用給定年份的函數。 –

+0

您必須將實際值作爲參數傳遞而不是類型。請嘗試... if isLeapYear(year:year)== true – slashburn

+1

爲什麼重新發明輪子?類「Calender」和「DateComponents」提供各種日期數學。 – vadian

回答

0

當你調用一個函數,你應該輸入它的價值

if isLeapYear(year: year) == true { 

if isLeapYear(year: Int(1)) == true { 
+1

'== true'是多餘的。 isLeapYear返回一個Bool –

+1

「Int(1)」的含義是什麼? '1'已經是'Int'了。 – rmaddy

0

您需要將參數的值添加到函數中,而不是參數的類型。

在這裏,我認爲你需要使用nextDay函數的參數。

當您調用具有參數的函數時,需要爲函數參數提供一個值,因爲該值將在函數內部使用。值類型應該與參數類型相同。在這種情況下,Int。

試試這個:

func isLeapYear(year: Int) -> Bool { 

    if year % 4 != 0{ 
     return false 
    } 
    else if year % 100 != 0{ 
     return true 
    } 
    else if year % 400 != 0{ 
     return false 
    } 
    else{ 
     return true 
    } 
} 

func nextDay(year: Int, month: Int, day: Int) -> (year: Int, month: Int, day: Int) { 

    // Your function call 
    if isLeapYear(year: year) == true { 
     // Your code 
    } 

    // Hardcorded values (year 2, month 2, days 2) 
     return (2, 2, 2) 
    } 
+0

對我不適用:/ – NMM

+0

首先將代碼複製並粘貼到操場上,然後檢查輸出。然後用你的代碼開發代碼。如果在這裏得到錯誤粘貼。 –

相關問題