swift guard關鍵字詳解及使用。本站提示廣大學習愛好者:(swift guard關鍵字詳解及使用)文章只能為提供參考,不一定能成為您想要的結果。以下是swift guard關鍵字詳解及使用正文
投稿:lqh
這篇文章主要介紹了swift guard關鍵字詳解及使用的相關資料,需要的朋友可以參考下swift guard關鍵字詳解及使用
Swift提供guard關鍵字,guard關鍵字可以簡化繁瑣的判斷邏輯
func buy( money: Int , price: Int , capacity: Int , volume: Int){
if money >= price{
if capacity >= volume{
print("I can buy it!")
print("\(money-price) Yuan left.")
print("\(capacity-volume) cubic meters left")
}
else{
print("No enough capacity")
}
}
else{
print("Not enough money")
}
}
以上代碼用guard關鍵字簡化代碼風格
func buy2( money: Int , price: Int , capacity: Int , volume: Int){
guard money >= price else{
print("Not enough money")
return
}
guard capacity >= volume else{
print("Not enough capacity")
return
}
print("\(money-price) Yuan left.")
print("\(capacity-volume) cubic meters left")
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!