简体   繁体   English

UIContentSizeCategory不符合Comparable吗?

[英]UIContentSizeCategory does not conform to Comparable?

It says in the type signature in UIKit that UIContentSizeCategory conforms to the Comparable protocol. 它在UIKit的类型签名中说UIContentSizeCategory符合Comparable协议。

The type signature is: 类型签名为:

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

    public init(rawValue: String)
}

So how come I get this nasty stack trace when I try to compare them? 那么,当我尝试比较它们时,如何得到此讨厌的堆栈跟踪信息?

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
            ^

When I try to write my own extension to make UIContentSizeCategory conform to Comparable I get an error that it already conforms. 当我尝试编写自己的扩展名以使UIContentSizeCategory符合Comparable ,出现一个错误,即它已经符合要求。

The goal here is to be able to check if a size is below a certain threshold and clip some text if it is. 此处的目标是能够检查大小是否低于某个阈值,并裁剪一些文本。 How do I fix this? 我该如何解决?

So, although the docs and signature claim conformance, it does not conform. 因此,尽管文档和签名声称符合,但不符合。

First time around I tried this: 我第一次尝试这样做:

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

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

it threw an error because of the redundant conformance to Comparable 它由于与Comparable的冗余一致性而抛出错误

When I tried it without: 当我尝试以下操作时:

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

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

Magic! 魔法! Works! 作品!

Full code (just in case you were wondering): 完整代码(以防万一您想知道):

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM