Swift 3.0:" objc_setAssociatedObject"问题

时间:2017-04-04 04:32:47

标签: ios swift3 objective-c-runtime swift3.1

import UIKit
import ObjectiveC

var SubRowObjectKey: String = "subRow"
extension IndexPath {

    var subRow: Int {
        get {
            let subRowObj = objc_getAssociatedObject(self, &SubRowObjectKey)

            if subRowObj != nil {
                return (subRowObj as! Int)
            }

            return 0
        }
        set {
            let subRowObj = (newValue as NSInteger)
            objc_setAssociatedObject(self, &SubRowObjectKey, subRowObj, .OBJC_ASSOCIATION_RETAIN)

        }
    }

    static func indexPathForSubRow(_ subRow: NSInteger, inRow row: NSInteger, inSection section: NSInteger) -> IndexPath {

        var indexPath = IndexPath(row: row, section: section)
        indexPath.subRow = (subRow as Int)
        print(subRow)
        print(indexPath.subRow)
        return indexPath
    }
}

let indexPath = IndexPath.indexPathForSubRow(5, inRow: 1, inSection: 2)
print(indexPath.subRow)

enter image description here

  • 在静态函数indexPathForSubRow中 - > '子行'计数= 5(附图中的行号:30)
  • 但是在将subRow分配给indexPath.subRow后,' indexPath.subRow' count = 0而不是5(附图中的29号线和31号线)
  • 在Xcode版本8.2.1&中测试Swift 3.0

    任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

IndexPath是一个结构,不支持关联的对象。 您可以通过直接尝试回读设置对象来轻松地在设置器中进行检查:

set {
    let subRowObj = (newValue as NSInteger)
    objc_setAssociatedObject(self, &SubRowObjectKey, subRowObj, .OBJC_ASSOCIATION_RETAIN)
    let subRowObj2 = objc_getAssociatedObject(self, &SubRowObjectKey)
    print(subRowObj2 ?? "nil") // prints "nil"
}

即使setter 中的代码有效,整个构造仍然不成立:由于传输/分配时复制了结构(至少在修改时)通过 copy on write 机制),您的关联对象将不会包含在该副本中,因此您迟早会丢失该信息。

尽管如此, 可以扩展IndexPath而不是扩展NSIndexPath,而{<1}}可以扩展$resources = Resource::withCount(['votes' => function ($query) { $query->where('vote_id', '=', '2'); //count positive votes only }])->get(); 然后工作正常 - 但我想这不是你想要的,因为你想影响从表视图中获取的IndexPath ...

答案 1 :(得分:0)

根据maddy的回答,这是我的IndexPath扩展,它添加了一个subRow属性。

extension IndexPath {

    init(subRow: Int, row: Int, section: Int) {
        self.init(indexes: [IndexPath.Element(section), IndexPath.Element(row), IndexPath.Element(subRow)])
    }

    var subRow: Int {
        get { return self[index(at: 2)] }
        set { self[index(at: 2)] = newValue }
    }

    var row: Int {
        get { return self[index(at: 1)] }
        set { self[index(at: 1)] = newValue }
    }

    var section: Int {
        get { return self[index(at: 0)] }
        set { self[index(at: 0)] = newValue }
    }

    private func index(at idx: Int) -> IndexPath.Index {
        return self.startIndex.advanced(by: idx)
    }

}