作为 NS_OPTIONS 迁移至 GMSPlaceField
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
从 Places SDK for iOS 的 4.0.0 版开始,GMSPlaceField
类型现在采用 NS_OPTIONS
宏。SDK 的 3.x 版本具有 GMSPlaceField
作为 NS_ENUM
,并且仍受支持。
如果您使用的是 Objective-C:则不会有任何影响。您可以继续像以前一样使用 GMSPlaceField
。
如果您使用的是 Swift:如果您使用 GMSPlaceField(rawValue:)
语法作为构造函数,您的实现将会中断,因为旧版 SDK 将其作为可选的 GMSPlaceField?
返回。版本 4.0.0 及更高版本会返回非可选的 GMSPlaceField
值;对可选值执行的任何操作(例如条件检查或强制解封装)都会失败。此外,您现在可以使用数组语法来组合 GMSPlaceField
迁移代码
某些条件解封装或强制解封装语法在 Swift 中会失效。以下示例展示了如何修复这些问题,还演示了如何使用数组语法来声明 GMSPlaceField
:
条件解封装
以下示例中的语句展示了如何使用 if
创建需要条件解封装的 GMSPlaceField
数组。这会导致编译器错误(“条件绑定的初始值设定项必须具有 Optional 类型,而不是 GMSPlaceField
”)。
// Before.
if let field = GMSPlaceField(
rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue
) { // Do something }
如需解决此问题,请移除 if
语句,如下所示:
// After.
let field = GMSPlaceField(
rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue
)
// Do something
您还可以使用数组语法,如下所示:
let field = [GMSPlaceField.name, GMSPlaceField.photos]
// or
let field: GMSPlaceField = [.name, .photos]
强制解封
以下示例中的语句展示了如何将 GMSPlaceField
用作非可选类型。这会导致编译器错误(“Cannot force unwrap value of non-optional type GMSPlaceField.”):
// Before.
let field = GMSPlaceField(
rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue
)!
如需解决此问题,请使用 GMSPlaceField
作为可选类型,如下所示:
// After.
let field = GMSPlaceField(
rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue
)
您还可以使用数组语法,如下所示:
let field = [GMSPlaceField.name, GMSPlaceField.photos]
// or
let field: GMSPlaceField = [.name, .photos]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Migrating to GMSPlaceField as NS_OPTIONS\n\nBeginning with Version 4.0.0 of the Places SDK for iOS, the `GMSPlaceField`\ntype now uses the `NS_OPTIONS` macro. Versions 3.x of the SDK have `GMSPlaceField`\nas `NS_ENUM` and are still supported.\n\n- **If you are using Objective-C:** there are no implications. You can continue\n using `GMSPlaceField` as before.\n\n- **If you are using Swift:** Your implementation will break if you are using\n the `GMSPlaceField(rawValue:)` syntax as the constructor, which older versions\n of the SDK returned as an optional `GMSPlaceField?`. Version 4.0.0 and higher\n returns a non-optional `GMSPlaceField` value; any operations that are\n performed on the optional, such as conditional checks or force unwraps, will\n fail. In addition, you may now use array syntax to combine `GMSPlaceField`s\n\nMigrating your code\n-------------------\n\nCertain conditional unwrapping or force unwrapping syntaxes will break in Swift.\nThe following examples show how to fix these issues, and also demonstrate using\narray syntax to declare `GMSPlaceField`:\n\n### Conditional unwrapping\n\nThe statement in the following example shows using `if` to create a\n`GMSPlaceField` array which requires conditional unwrapping. This will result\nin a compiler error (\"Initializer for conditional binding must have Optional\ntype, not `GMSPlaceField`\".) \n\n // Before.\n if let field = GMSPlaceField(\n rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue\n ) { // Do something }\n\nTo fix this issue, remove the `if` statement, as shown here: \n\n // After.\n let field = GMSPlaceField(\n rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue\n )\n // Do something\n\nYou can also use array syntax, as shown here: \n\n let field = [GMSPlaceField.name, GMSPlaceField.photos]\n // or\n let field: GMSPlaceField = [.name, .photos]\n\n### Force unwrapping\n\nThe statement in the following example shows using `GMSPlaceField` as a non-\noptional type. This will result in a compiler error (\"Cannot force unwrap value\nof non-optional type GMSPlaceField.\"): \n\n // Before.\n let field = GMSPlaceField(\n rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue\n )!\n\nTo fix this issue, use `GMSPlaceField` as an optional type, as shown here: \n\n // After.\n let field = GMSPlaceField(\n rawValue: GMSPlaceField.name.rawValue | GMSPlaceField.photos.rawValue\n )\n\nYou can also use array syntax, as shown here: \n\n let field = [GMSPlaceField.name, GMSPlaceField.photos]\n // or\n let field: GMSPlaceField = [.name, .photos]"]]