【问题标题】:How would I implement this Objective-C Line in Swift?我将如何在 Swift 中实现这个 Objective-C 行?
【发布时间】:2020-10-27 09:01:41
【问题描述】:

在名为UIScrollView-InfiniteScroll(链接here)的库中,您可以在Classes/UIScrollView+InfiniteScroll.m 中找到行代码:

static const void *kPBInfiniteScrollStateKey = &kPBInfiniteScrollStateKey;

如何在 Swift 中重写这一行?有没有可能?

【问题讨论】:

    标签: objective-c swift pointers reference uikit


    【解决方案1】:

    如果您包含原始评论,它会有很大帮助

    // Keys for values in associated dictionary
    static const void *kPBInfiniteScrollStateKey = &kPBInfiniteScrollStateKey;
    

    您可以在 Swift 中使用这样的关联对象,感谢 erhatsezer https://gist.github.com/serhatsezer/4abe382229b001b37b1ee2f46e62b90f#file-productdetail-extension-swift

    private var associateKey: Void?
    
    extension ProductDetail {
        var totalSumOfBasket: String? {
            get {
                return objc_getAssociatedObject(self, &associateKey) as? String
            }
            set {
                objc_setAssociatedObject(self, &associateKey, newValue, .OBJC_ASSOCIATION_RETAIN)
            }
        }
    }
    

    【讨论】:

    • 你真是个天才,非常感谢。我自己不知道该怎么做。虽然这是无证的,这绝对是荒谬的。
    【解决方案2】:

    最基本的方法是使用https://swiftify.com 工具。将 objc 转换为 swift 的最简单方法。

    对于无限滚动视图,我发现的最佳解决方案是https://github.com/rebeloper/DScrollview,它完全是用 swift 编写的。

    【讨论】:

    • 我最初尝试使用 Swiftify 和其他一些转译器将我提到的项目移植到 Swift 中,但是两种解决方案都在 Xcode 中给了我语法错误,这就是我最终提出这个问题的原因。非常感谢无限滚动视图解决方案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 2018-10-15
    • 2017-03-19
    • 2010-10-27
    相关资源
    最近更新 更多