lyb5834 / YBAttributeTextTapForSwfit

一行代码添加文本点击事件(swfit4.2版本)/a fast way to implement click event text(for swfit4.2)
MIT License
52 stars 15 forks source link

swift4中有一个地方报错, 在存储属性那里用全局变量是有问题的. #6

Open GG526 opened 6 years ago

GG526 commented 6 years ago
  1. swift4中只需改这个地方就可以兼容.
    func nsRange(from range: Range<String.Index>) -> NSRange {
    //        let from = range.lowerBound.samePosition(in: utf16)
    //        let to = range.upperBound.samePosition(in: utf16)
        // utf16.distance(from: utf16.startIndex, to: from)  //utf16.distance(from: from, to: to)
        return NSRange.init(range, in: self)
    //        return NSRange(location:self.startIndex ,length: )
    }

    注释的地方是您的代码。

2、在extension中添加储存属性可以使用runtime

private func setIsTapAction(isTapAction: Bool) {
        objc_setAssociatedObject(self, &AssociatedKeys.tapAction, isTapAction as Bool, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }

    private func getIsTapAction() -> Bool {
        let isTap = objc_getAssociatedObject(self, &AssociatedKeys.tapAction) as? Bool
        return isTap != nil ? isTap! : true
    }

如果用原来的实现方式会出现第二地方添加的action会清空上一次的添加的.

lyb5834 commented 6 years ago

多谢指出