我正在发展的逻辑,
图书页与自由页数组索引的比较。
freePageArr = ["1", "5", "6", "12", "14", "15"]
self.bookPage = 1
func checkingPay(_ bookPage: Int) {
let freePage = bookDetail?.bookFreePageList
let freePageArr = freePage?.components(separatedBy: ",")
//fail logic....
if <<I need code..!!>> {
print("Hello!! Free Page.")
print("currentPage :", self.bookPage)
} else {
print("Sorry!! Paid Page.")
print("CurrentPage :", self.bookPage)
}
}
如果free
pageArr
的值与self.bookPage
的值相同,则printprint (" Hello !! Free Page. ")
如果free
pageArr
索引的值与self.bookPage
的值不同,我想打印print (" Sorry !! Paid Page. ")
日志。 最佳答案
您只需使用contains(_:)
onfreePageArr
检查其中是否存在bookPage
。
此外,由于components(separatedBy:)
给出array
的String
,您需要使用Int
将其转换为array
compactMap(_:)
,以匹配其中的bookPage
值。
func checkingPay(_ bookPage: Int) {
let freePage: String? = bookDetail?.bookFreePageList //it must be like "1,5,6,12,14,15"
if let freePageArr = freePage?.components(separatedBy: ",").compactMap({ Int($0) }) {
if freePageArr.contains(bookPage) {
print("Hello!! Free Page.")
print("currentPage :", bookPage)
} else {
print("Sorry!! Paid Page.")
print("CurrentPage :", bookPage)
}
}
}