YBAttributeTextTapForSwfit icon indicating copy to clipboard operation
YBAttributeTextTapForSwfit copied to clipboard

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

Open GG526 opened this issue 6 years ago • 1 comments

  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会清空上一次的添加的.

GG526 avatar Dec 05 '17 06:12 GG526