请稍侯

swift - 数据截切

14 April 2023

Swift - 数据截切

    func subArray<T>(from startIndex: Int, to endIndex: Int) -> [T]? where T == Element {
        // check start not out bounds
        guard startIndex >= 0 && startIndex < self.count else {
            return nil
        }

        // check end not out bounds
        guard endIndex > 0 && endIndex <= self.count else {
            return nil
        }

        // get rang
        return Array(self[startIndex..<endIndex])
    }