我正在尝试为仅在ios15中可用的api进行向后兼容的扩展。以下代码在调试生成中成功运行,但在发布生成中则不然。所以我想知道是否有一些设置我可以更改,以便在版本构建或任何其他方式中工作
扩展:
public struct Backport<Content> {
public let content: Content
public init(_ content: Content) {
self.content = content
}
}
extension View {
var backport: Backport<Self> { Backport(self) }
}
extension Backport where Content: View {
@ViewBuilder func badge(_ count: Int) -> some View {
if #available(iOS 15, *) {
content.badge(count)
} else {
content
}
}
}
正在运行扩展并给出错误的代码:
NavigationView{
ActivityView()
.backport.badge(10) <-- Error
}
它给出以下错误:
线程1:EXC_BAD_ACCESS(代码=1,地址=0x0(
我也有向后兼容性的问题,但用这个扩展解决了这个问题:
// MARK: - modify
/// A modify extension to handle iOS versions.
public extension View {
func modify<Content>(@ViewBuilder _ transform: (Self) -> Content) -> Content {
transform(self)
}
}
用法:
TextField(title.rawValue + " username", text: $text.bound)
.disableAutocorrection(true)
.modify {
if #available(iOS 15, *) {
$0
.textInputAutocapitalization(.none)
} else {
$0
.autocapitalization(.none)
}
}