微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数组 – 2D数组扩展Swift 3.1.1

我试图在 Swift 3.1.1中创建一个Array扩展,它支持将对象添加到2D Array中的某个索引,即使该数组尚未填充.扩展还应该提供在某个indexPath上获取对象的能力.我在Swift 2中有这个代码,但我似乎无法将它迁移到Swift 3.这是Swift 2代码

extension Array where Element: _ArrayProtocol,Element.Iterator.Element: Any {

    mutating func addobject(_ anObject : Element.Iterator.Element,toSubarrayAtIndex idx : Int) {
        while self.count <= idx {
            let newSubArray = Element()
            self.append(newSubArray) 
        }

        var subArray = self[idx]
        subArray.append(anObject)
    }

    func objectAtIndexPath(_ indexPath: IndexPath) -> Any {
        let subArray = self[indexPath.section]
        return subArray[indexPath.row] as Element.Iterator.Element
    }
}

代码取自此answer.

解决方法

正如Martin在Swift 3.1中所说的 in his answer here,_ArrayProtocol is no longer public,因此意味着你不能将它用作扩展中的约束.

在您的情况下,一个简单的替代方法是将Array的Element约束为RangeReplaceableCollection – 它们都定义了一个init()需求,意味着“空集合”,以及一个append(_ :)方法,以便将元素添加到集合中.

extension Array where Element : RangeReplaceableCollection {

    typealias InnerCollection = Element
    typealias InnerElement = InnerCollection.Iterator.Element

    mutating func fillingAppend(
        _ newElement: InnerElement,toSubCollectionAtIndex index: Index) {

        if index >= count {
            append(contentsOf: repeatElement(InnerCollection(),count: index + 1 - count))
        }

        self[index].append(newElement)
    }
}

另请注意,我们将append作为单个调用进行(使用append(contentsOf :),确保我们只需要调整外部数组的大小一次.

对于从给定IndexPath获取元素的方法,您可以将内部元素类型约束为具有Int索引的Collection

// Could also make this an extension on Collection where the outer Index is also an Int.
extension Array where Element : Collection,Element.Index == Int {

    subscript(indexPath indexPath: IndexPath) -> Element.Iterator.Element {
        return self[indexPath.section][indexPath.row]
    }
}

请注意,我已经使它成为下标而不是方法,因为我觉得它更符合Array的API.

你现在可以简单地使用这些扩展:

var arr = [[Int]]()

arr.fillingAppend(6,toSubCollectionAtIndex: 3)
print(arr) // [[],[],[6]]

let indexPath = IndexPath(row: 0,section: 3)
print(arr[indexPath: indexPath]) // 6

虽然当然如果您事先知道外部数组的大小,fillAppend(_:toSubCollectionAtIndex :)方法是多余的,因为您可以通过以下方式创建嵌套数组:

var arr = [[Int]](repeating: [],count: 5)

这将创建一个包含5个空[Int]元素的[[Int]]数组.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐