如何处理 Swift UI 版本兼容以支持旧设备?
How to deal with Swift UI version compatible to support older devices?
我正在 Xcode 12 beta WatchOS 7 上开发 Apple Watch 应用程序。
我也打算支持 watchOS 6。
以下代码有错误,不知如何处理,属于SwiftUI问题:
struct CompactStockListView: View {
var body: some View {
NavigationView {
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
.navigationBarTitle(Text("Landmarks"))
}
}
}
Xcode 注意错误:
'NavigationView' is only available in application extensions for watchOS 7.0 or newer
下一步:
尝试此解决方案但未成功:
struct CompactStockListView: View {
var body: some View {
getListSafe() // Error
}
func getListSafe() -> View { // Error tooo
if #available(watchOSApplicationExtension 7.0, *) {
return NavigationView {
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
.navigationBarTitle(Text("Landmarks"))
}
} else {
// Fallback on earlier versions
return List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
}
}
}
尝试以下方法
@ViewBuilder
func getListSafe() -> some View {
if #available(watchOSApplicationExtension 7.0, *) {
NavigationView {
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
.navigationBarTitle(Text("Landmarks"))
}
} else {
// Fallback on earlier versions
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
}
}
我正在 Xcode 12 beta WatchOS 7 上开发 Apple Watch 应用程序。
我也打算支持 watchOS 6。
以下代码有错误,不知如何处理,属于SwiftUI问题:
struct CompactStockListView: View {
var body: some View {
NavigationView {
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
.navigationBarTitle(Text("Landmarks"))
}
}
}
Xcode 注意错误:
'NavigationView' is only available in application extensions for watchOS 7.0 or newer
下一步: 尝试此解决方案但未成功:
struct CompactStockListView: View {
var body: some View {
getListSafe() // Error
}
func getListSafe() -> View { // Error tooo
if #available(watchOSApplicationExtension 7.0, *) {
return NavigationView {
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
.navigationBarTitle(Text("Landmarks"))
}
} else {
// Fallback on earlier versions
return List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
}
}
}
尝试以下方法
@ViewBuilder
func getListSafe() -> some View {
if #available(watchOSApplicationExtension 7.0, *) {
NavigationView {
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
.navigationBarTitle(Text("Landmarks"))
}
} else {
// Fallback on earlier versions
List(getStocks(), id: \.id) { stock in
CompactStockRowView(stock: stock)
.padding(.vertical, 6)
}
}
}