Ios UIContentSizeCategory不符合可比性?
它在UIKit中的类型签名中指出,Ios UIContentSizeCategory不符合可比性?,ios,swift,uikit,Ios,Swift,Uikit,它在UIKit中的类型签名中指出,UIContentSizeCategory符合可比协议 类型签名为: public struct UIContentSizeCategory : RawRepresentable, Equatable, Hashable, Comparable { public init(rawValue: String) } 那么,当我试图比较它们时,为什么会得到这种讨厌的堆栈跟踪呢 po UIContentSizeCategory.small < UICo
UIContentSizeCategory
符合可比
协议
类型签名为:
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
^
po-UIContentSizeCategory.smallBool
^
Swift.>=:12:13:注意:候选人完全匹配
公共函数>=(左:T,右:T)->布尔,其中T:可比
^
Swift.>=:1:13:注意:候选人完全匹配
public func>=(左:T,右:T)->Bool其中T:_SwiftNewtypeWrapper,T.RawValue:可比
^
Swift.compariable:158:24:注意:多个名为“>”的匹配函数,类型为“(UIContentSizeCategory,UIContentSizeCategory)->Bool”
公共静态函数>(左:自,右:自)->Bool
^
Swift.>:10:13:注意:候选人完全匹配
公共函数>(左:T,右:T)->布尔,其中T:可比
^
Swift.>:1:13:注意:候选人完全匹配
公共函数>(左:T,右:T)->布尔式,其中T:_SwiftNewtypeWrapper,T.RawValue:可比
^
当我试图编写自己的扩展以使UIContentSizeCategory
符合Comparable
时,我得到一个错误,它已经符合了
这里的目标是能够检查大小是否低于某个阈值,如果低于某个阈值,则可以剪裁一些文本。如何解决此问题?因此,尽管文档和签名声明一致,但并不一致 我第一次尝试这个:
extension UIContentSizeCategory: Comparable {
static func <(lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
return true
}
static func >(lhs: UIContentSizeCategory, rhs: UIContentSizeCategory) -> Bool {
return true
}
}
魔法!工作
完整代码(以防万一您想知道):
扩展UIContentSizeCategory{
静态变量orderedSizes:[UIContentSizeCategory]{
返回,返回,
小的
.accessibilityMedium,
.中等,
.accessibilityLarge,
.大型,
.accessibilityExtraLarge,
.超大,
.可访问性超大型,
.特大型,
.超大,
.AccessibilityExtra超大
]
}
静态函数<(lhs:UIContentSizeCategory,rhs:UIContentSizeCategory)->Bool{
变量大小=订单大小
while size.contains(lhs){
大小。removeFirst()
}
返回大小。包含(rhs)
}
静态函数>(lhs:UIContentSizeCategory,rhs:UIContentSizeCategory)->Bool{
变量大小=订单大小
while size.contains(lhs){
大小。removeLast()
}
返回大小。包含(rhs)
}
静态函数布尔{
guard lhs!=rhs else{返回真值}
返回左侧<右侧
}
静态函数>=(lhs:UIContentSizeCategory,rhs:UIContentSizeCategory)->Bool{
guard lhs!=rhs else{返回真值}
返回左侧>右侧
}
}
您问题中的前两组代码是相同的。一个怎么工作,另一个怎么不工作?啊,我明白了,已经修好了。问题仍然存在,文档声称没有任何实现就符合要求。
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
}
}