作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
它在 UIKit 的类型签名中表示 UIContentSizeCategory
符合 Comparable
协议(protocol)。
类型签名是:
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
时,我收到一条错误,表明它已经符合要求。
这里的目标是能够检查大小是否低于某个阈值,如果低于则剪辑一些文本。我该如何解决这个问题?
最佳答案
因此,尽管文档和签名声称符合,但实际上并不符合。
我第一次尝试这个:
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
}
}
关于ios - UIContentSizeCategory不符合Comparable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44317003/
我想使用不同的系统文本大小选项(包括辅助功能大小)轻松测试我的应用。这些可以在设置应用程序中设置(显示和亮度 => 文本大小或常规 => 辅助功能 => 较大的文本)。 目前我能找到的唯一方法是进入“
我是一名优秀的程序员,十分优秀!