强制解包位于 Swift 中的 `flatMap` 内

huangapple go评论51阅读模式
英文:

Forced unwrapping inside flatMap in combine swift

问题

I am trying to do some forced unwrapping inside my flatMap function but getting following error on return nil line:

我在我的flatMap函数内尝试进行一些强制解包,但在return nil行时出现以下错误:

> 'nil' requires a contextual type

> 'nil'需要上下文类型

Following is my code:

以下是我的代码:

func fetchOrgs() -> AnyPublisher<[THOrganizationV2?], THError> {
let locale = userInformationProvider.defaultOrganizationLocale
let orgID = organizationProvider.currentOrganizationID ?? ""

return thProvider.fetchTHOrgs(locale: locale, orgID: orgID).flatMap { orgs in
    guard let org = orgs.first else {
        return nil// error here
    }
    if org.type == .xpert {
        return self.prepareThURL(with: org, orgID: orgID)
    }
    return THOrganizationV2(serviceURL: URL(string: org.url), type: OrganizationType(rawValue: (org.type)!.rawValue) ?? .aaa, authenticationToken: org.authenticationToken)
}.eraseToAnyPublisher()

}

Signature of function called above in thProvider:

上面在thProvider中调用的函数的签名:

func fetchTHOrgs(locale: String, orgID: String?) -> AnyPublisher<[thOrganizationEntityV2], THError>

I have tried different combinations of map and flatMap but nothing seems to be working for me.

我尝试了不同的map和flatMap组合,但似乎没有任何方法适用于我。

Edit:
My goal here is to convert an array of thOrganizationEntityV2 to an array of THOrganizationV2 and handle forced unwrapping too.

编辑:
我的目标是将thOrganizationEntityV2数组转换为THOrganizationV2数组,并处理强制解包。

英文:

I am trying to do some forced unwrapping inside my flatMap function but getting following error on return nil line:

> 'nil' requires a contextual type

Following is my code:

func fetchOrgs() -&gt; AnyPublisher&lt;[THOrganizationV2?], THError&gt; {
    let locale = userInformationProvider.defaultOrganizationLocale
    let orgID = organizationProvider.currentOrganizationID ?? &quot;&quot;
    
    return thProvider.fetchTHOrgs(locale: locale, orgID: orgID).flatMap { orgs in
        guard let org = orgs.first else {
            return nil// error here
        }
        if org.type == .xpert {
            return self.prepareThURL(with: org, orgID: orgID)
        }
        return THOrganizationV2(serviceURL: URL(string: org.url), type: OrganizationType(rawValue: (org.type)!.rawValue) ?? .aaa, authenticationToken: org.authenticationToken)
    }.eraseToAnyPublisher()
}

Signature of function called above in thProvider:

func fetchTHOrgs(locale: String, orgID: String?) -&gt; AnyPublisher&lt;[thOrganizationEntityV2], THError&gt;

I have tried different combinations of map and flatMap but nothing seems to be working for me.

Edit:
My goal here is to convert array of thOrganizationEntityV2 to array of THOrganizationV2 and wants to handle forced unwrapping too.

Signature for prepareThURL:

private func prepareThURL(with org: THOrganizationEntityV2, orgID: String?) -&gt; THOrganizationV2?

答案1

得分: 1

以下是您要翻译的内容:

编译器正在查看flatMap中的块,无法确定nil代表的类型。

flatMap运算符期望其闭包返回某种类型的Publisher,而不是可选项。在您的情况下,flatMap似乎是错误的运算符。您的代码似乎可能返回THOrganizationV2?,这不是一个Publisher

在块中,编译器进行到这一步:

.flatMap { orgs in
    guard let org = orgs.first else { return nil }

并说:“嗯...我在一个没有声明返回类型的闭包中,你给了我一个nil。我不知道这是哪种类型的nil。它是Int?Float?OmpaLoompa?...我没有上下文来确定这个特定的nil对应于什么类型。”

您可以通过为闭包声明返回类型来修复它:

.flatMap { orgs -> THOrganizationV2? in
    guard let org = orgs.first else { return nil }

或直接为nil提供上下文类型:

.flatMap { orgs in
    guard let org = orgs.first else { return nil as THOrganizationV2? }

但再次强调,flatMap发布器运算符期望返回类型是一个发布器。我认为您的THOrganizationV2不是Publisher

从您的代码中看,似乎您想要的是这样的东西:

return thProvider
   .fetchTHOrgs(locale: locale, orgID: orgID)
   .map { orgs in
      return orgs.map { org -> THOrganizationV2? in
          guard let org = orgs.first else { nil }

          if org.type == .xpert {
              return self.prepareThURL(with: org, orgID: orgID)
          }

          return THOrganizationV2(
            serviceURL: URL(string: org.url), 
            type: OrganizationType(rawValue: (org.type)!.rawValue) ?? .aaa,
            authenticationToken: org.authenticationToken
          )
      }
  }

(我将此输入到答案编辑器中,而不是编译器,所以可能需要一些工作)

我将flatMap替换为map。对于每个传入的orgs数组,系统将其映射为一个THOrganizationV2?数组,这些数组成为发布器的输出,因此fetchOrgs应该与其类型签名匹配。

英文:

The compiler is looking at the block in the flatMap and can't determine what type the nil represents.

The flatMap operator expects its closure to return a Publisher of some sort, not an optional. In your case flatMap seems to be the wrong operator. Your code seems like it might be returning THOrganizationV2? which is not a Publisher.

In the block the compiler gets this far:

.flatMap { orgs in
    guard let org = orgs.first else { return nil }

and says "hmmm... I'm in a closure that has no declared return type and you're giving me a nil. I don't know what flavor of nil that is. Is it an Int?, a Float?, a OmpaLoompa?... I have no context to figure out what Type that particular nil corresponds to."

You might fix it by declaring a return type for the closure:

.flatMap { orgs -&gt; THOrganizationV2? in
    guard let org = orgs.first else { return nil }

or by directly providing a contextual type for the nil:

.flatMap { orgs in
    guard let org = orgs.first else { return nil as THOrganizationV2? }

But once again, the flatMap publisher operator expects the return type to be a publisher. I don't think your THOrganizationV2 is a Publisher.

Looking at the type signatures in your code, it seems that what you might want is something like this:

   return thProvider
     .fetchTHOrgs(locale: locale, orgID: orgID)
     .map { orgs in
        return orgs.map { org -&gt; THOrganizationV2? in
            guard let org = orgs.first else { nil }

            if org.type == .xpert {
                return self.prepareThURL(with: org, orgID: orgID)
            }

            return THOrganizationV2(
              serviceURL: URL(string: org.url), 
              type: OrganizationType(rawValue: (org.type)!.rawValue) ?? .aaa,
              authenticationToken: org.authenticationToken
            )
        }
    }

(I typed this into the answer editor, not a compiler so some work may be needed)

I've replaced flatMap with map. For each array of orgs that come in, the system maps it to an array of THOrganizationV2? and those become the output of the publisher so fetchOrgs should match it's type signature.

huangapple
  • 本文由 发表于 2023年5月17日 09:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定