Swift'te Bir aralığın bir değer içerip içermediğini kontrol etmenin 4 yolu
Swift çok güçlü bir programlama dilidir ve dilin doğuşundan beri geliştiricinin hayatını çok kolaylaştırmaktadır. Bu konuda, bir değerin bir aralıkta olup olmadığını kontrol etmenin 4 farklı yolunu göstereceğim.
Bir değere sahip olduğumuzu varsayalım = 10 ve 4 ile 20 arasında olup olmadığını kontrol etmek istiyoruz.
Bir değere sahip olduğumuzu varsayalım = 10 ve 4 ile 20 arasında olup olmadığını kontrol etmek istiyoruz.
Pattern eşleştirme operatörü ~ =
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let value = 10 | |
if 4...20 ~= value { | |
print("10 is in range 4-20") | |
} else { | |
print("10 is not in range 4-20") |
.contains method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let value = 10 | |
if (4...20).contains(value) { | |
print("10 is in range 4-20") | |
} else { | |
print("10 is not in range 4-20") | |
} |
if case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let value = 10 | |
if case 4...20 = value { | |
print("10 is in range 4-20") | |
} else { | |
print("10 is not in range 4-20") | |
} |
switch case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let value = 10 | |
switch value { | |
case 4...20: | |
print("10 is in range 4-20") | |
default: | |
print("10 is not in range 4-20") | |
} |
Hiç yorum yok:
Yorum Gönder
Yorumunuz için teşekkür ederim.