我正在发展的逻辑,
图书页与自由页数组索引的比较。

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)
        }
    }

如果freepageArr的值与self.bookPage的值相同,则printprint (" Hello !! Free Page. ")
如果freepageArr索引的值与self.bookPage的值不同,我想打印print (" Sorry !! Paid Page. ")日志。

最佳答案

您只需使用contains(_:)onfreePageArr检查其中是否存在bookPage
此外,由于components(separatedBy:)给出arrayString,您需要使用Int将其转换为arraycompactMap(_:),以匹配其中的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)
        }
    }
}

10-05 20:02