为可选参数传递 nil 会在 Shopify SDK 中引发错误

问题描述 投票:0回答:1

这个问题与 Shopify 的 Buy SDK for iOS 有关。

我正在使用

checkoutCreate
突变创建结账。根据文档,
email
参数是可选的。但如果我将
nil
传递给它,API 调用就会失败。

let checkoutInput = Storefront.CheckoutCreateInput.create(
    email: .value(email), // `email` is an optional string and I'm passing `nil` for that parameter
    lineItems: .value(lineItemInput),
    note: .value(note),
    allowPartialAddresses: .value(true)
)

根据文档,

.value(:)
案例本身接受可选字符串值。

image from SDK docs

所以我不确定为什么如果我实际传入的话会导致API调用失败

nil
。任何有使用 Shopify SDK 经验的人都将不胜感激。

谢谢!

ios swift shopify shopify-storefront-api shopify-mobile-buy-sdk
1个回答
0
投票

我查看了 API,虽然它没有说明电子邮件字段是必需的,但我看到此问题已解决

这正是您正在处理的场景。它已于上个月开放,因此这可能是 API 的新问题。

我不知道这是否是您,但如果不是,可能值得对其进行监控以查看 Shopify 开发人员的反应。

对于您的具体问题,我建议检查电子邮件字段是否有价值,如果没有,请不要传入。

大致如下:

guard let userEmail = email, !userEmail.isEmpty else {
   let checkoutInput = Storefront.CheckoutCreateInput.create(
       lineItems: .value(lineItemInput),
       note: .value(note),
       allowPartialAddresses: .value(true)
     )
}

let checkoutInput = Storefront.CheckoutCreateInput.create(
       email: .value(email),
       lineItems: .value(lineItemInput),
       note: .value(note),
       allowPartialAddresses: .value(true)
     )
}

我假设您可以将这里的逻辑与您目前拥有的任何内容结合起来。

© www.soinside.com 2019 - 2024. All rights reserved.