UIContentSizeCategory不符合Comparable?

时间:2017-06-01 20:59:57

标签: ios swift uikit

在UIKit的类型签名中说UIContentSizeCategory符合Comparable协议。

类型签名是:

public struct UIContentSizeCategory : RawRepresentable, Equatable, Hashable, Comparable {

    public init(rawValue: String)
}

那么当我尝试比较它们时,为什么我会得到这个讨厌的堆栈跟踪?

po UIContentSizeCategory.small < UIContentSizeCategory.medium
error: warning: <EXPR>:12:9: warning: initialization of variable '$__lldb_error_result' was never used; consider replacing with assignment to '_' or removing it
    var $__lldb_error_result = __lldb_tmp_error
    ~~~~^~~~~~~~~~~~~~~~~~~~
    _

error: type 'UIContentSizeCategory' does not conform to protocol 'Comparable'
Swift.Comparable:144:24: note: multiple matching functions named '<=' with type '(UIContentSizeCategory, UIContentSizeCategory) -> Bool'
    public static func <=(lhs: Self, rhs: Self) -> Bool
                       ^

Swift.<=:10:13: note: candidate exactly matches
public func <=<T>(lhs: T, rhs: T) -> Bool where T : Comparable
            ^

Swift.<=:1:13: note: candidate exactly matches
public func <=<T>(lhs: T, rhs: T) -> Bool where T : _SwiftNewtypeWrapper, T.RawValue : Comparable
            ^

Swift.Comparable:151:24: note: multiple matching functions named '>=' with type '(UIContentSizeCategory, UIContentSizeCategory) -> Bool'
    public static func >=(lhs: Self, rhs: Self) -> Bool
                       ^

Swift.>=:12:13: note: candidate exactly matches
public func >=<T>(lhs: T, rhs: T) -> Bool where T : Comparable
            ^

Swift.>=:1:13: note: candidate exactly matches
public func >=<T>(lhs: T, rhs: T) -> Bool where T : _SwiftNewtypeWrapper, T.RawValue : Comparable
            ^

Swift.Comparable:158:24: note: multiple matching functions named '>' with type '(UIContentSizeCategory, UIContentSizeCategory) -> Bool'
    public static func >(lhs: Self, rhs: Self) -> Bool
                       ^

Swift.>:10:13: note: candidate exactly matches
public func ><T>(lhs: T, rhs: T) -> Bool where T : Comparable
            ^

Swift.>:1:13: note: candidate exactly matches
public func ><T>(lhs: T, rhs: T) -> Bool where T : _SwiftNewtypeWrapper, T.RawValue : Comparable
            ^

当我尝试编写自己的扩展程序以使UIContentSizeCategory符合Comparable时,我会收到一个已经符合的错误。

此处的目标是能够检查大小是否低于某个阈值并剪切一些文本(如果是)。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

因此,虽然文档和签名声称一致,但它不符合。

第一次我尝试了这个:

extension UIContentSizeCategory: Comparable {
    static func <(lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        return true
    }

    static func >(lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        return true
    }
}

由于与Comparable

的冗余一致性而引发错误

当我没有尝试时:

extension UIContentSizeCategory {
    static func <(lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        return true
    }

    static func >(lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        return true
    }
}

魔术!作品!

完整代码(以防万一你想知道):

extension UIContentSizeCategory {
    static var orderedSizes: [UIContentSizeCategory] {
        return [.extraSmall,
                    .small,
                    .accessibilityMedium,
                    .medium,
                    .accessibilityLarge,
                    .large,
                    .accessibilityExtraLarge,
                    .extraLarge,
                    .accessibilityExtraExtraLarge,
                    .extraExtraLarge,
                    .extraExtraExtraLarge,
                    .accessibilityExtraExtraExtraLarge
            ]
    }

    static func < (lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        var sizes = orderedSizes
        while sizes.contains(lhs) {
            sizes.removeFirst()
        }
        return sizes.contains(rhs)
    }

    static func > (lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        var sizes = orderedSizes
        while sizes.contains(lhs) {
            sizes.removeLast()
        }
        return sizes.contains(rhs)
    }

    static func <= (lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        guard lhs != rhs else { return true }

        return lhs < rhs
    }

    static func >= (lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
        guard lhs != rhs else { return true }

        return lhs > rhs
    }
}