Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Group()在SwiftUI中未被识别为不透明类型_Swift_Swiftui - Fatal编程技术网

Group()在SwiftUI中未被识别为不透明类型

Group()在SwiftUI中未被识别为不透明类型,swift,swiftui,Swift,Swiftui,我有一个带有if语句的函数,在任何一种情况下都返回一个Group(),但是SwiftUI编译器不将其识别为相同的类型。发生什么事了 获取错误: 函数声明一个不透明的返回类型,但 其主体没有匹配的基础类型 这是可能的解决办法。使用Xcode 12/iOS 14进行测试 @ViewBuilder private func cellWithContact(_ contact: CNContact) -> some View { let displayName: String? = CNC

我有一个带有if语句的函数,在任何一种情况下都返回一个
Group()
,但是SwiftUI编译器不将其识别为相同的类型。发生什么事了

获取错误:

函数声明一个不透明的返回类型,但 其主体没有匹配的基础类型


这是可能的解决办法。使用Xcode 12/iOS 14进行测试

@ViewBuilder
private func cellWithContact(_ contact: CNContact) -> some View {
    let displayName: String? = CNContactFormatter.string(from: contact, style: .fullName)
    if let displayName = displayName {
        Text(displayName)
    } else {
        EmptyView()  // or `Group {}`, but it is the same
    }
}

这是可能的解决办法。使用Xcode 12/iOS 14进行测试

@ViewBuilder
private func cellWithContact(_ contact: CNContact) -> some View {
    let displayName: String? = CNContactFormatter.string(from: contact, style: .fullName)
    if let displayName = displayName {
        Text(displayName)
    } else {
        EmptyView()  // or `Group {}`, but it is the same
    }
}

不透明类型仍然要求被调用方指定具体的单一类型,但在函数中有两种不同的返回类型:

return Group { Text(displayName) } // Group<Text> type
// and also
return Group { }                   // Group<EmptyView> type
  • 通过将空的更改为:

  • 不透明类型仍然要求被调用方指定具体的单一类型,但在函数中有两种不同的返回类型:

    return Group { Text(displayName) } // Group<Text> type
    // and also
    return Group { }                   // Group<EmptyView> type
    
  • 通过将空的更改为:

  • 其他答案都很好。我只想提到,如果您使用的是SwiftUI 2/iOS 14,您可以直接在
    @ViewBuilder
    块中使用控制流语句(如
    if let
    ):

    @ViewBuilder
    private func cellWithContact(_ contact: CNContact) -> some View {
        if let displayName = CNContactFormatter.string(from: contact, style: .fullName) {
            Text(displayName)
        }
    }
    

    而且不需要返回
    EmptyView()
    ,这使得代码更加清晰。

    其他答案都很好。我只想提到,如果您使用的是SwiftUI 2/iOS 14,您可以直接在
    @ViewBuilder
    块中使用控制流语句(如
    if let
    ):

    @ViewBuilder
    private func cellWithContact(_ contact: CNContact) -> some View {
        if let displayName = CNContactFormatter.string(from: contact, style: .fullName) {
            Text(displayName)
        }
    }
    

    而且无需返回
    EmptyView()
    ,这使代码更加清晰。

    谢谢!第一次看到需要
    @ViewBuilder
    ,是iOS14+?我将对此进行研究,但我的大多数视图生成器功能都没有这个功能。不,它从SwiftUI开始就可用。这就是为什么SwiftUI允许我们以声明的方式编写视图,其中只有一堆视图语句和条件“神奇地”生成了一个复合视图谢谢!第一次看到需要
    @ViewBuilder
    ,是iOS14+?我将对此进行研究,但我的大多数视图生成器功能都没有这个功能。不,它从SwiftUI开始就可用。这就是为什么SwiftUI允许我们以声明式的方式编写视图,其中只有一堆视图语句和条件“神奇地”生成了一个复合视图
    @ViewBuilder
    private func cellWithContact(_ contact: CNContact) -> some View {
        if let displayName = CNContactFormatter.string(from: contact, style: .fullName) {
            Text(displayName)
        }
    }