英文:
Swift Compiler Error when build ios app on Xcode
问题
首先,我使用的是 xcode 版本 13.2.1,当我尝试在终端/ xcode 中构建我的 iOS 应用程序时遇到了问题,以下是错误响应:
> /Users/user/.pubcache/hosted/pub.dev/shared_preferences_foundation-2.3.0/ios/Classes/SharedPreferencesPlugin.swift:58:22: 在条件中绑定变量需要一个初始化器
以下是出错的完整函数:
func getAllPrefs(prefix: String, allowList: [String]?) -> [String: Any] {
var filteredPrefs: [String: Any] = [:]
var allowSet: Set<String>?;
if let allowList {
allowSet = Set(allowList)
}
if let appDomain = Bundle.main.bundleIdentifier,
let prefs = UserDefaults.standard.persistentDomain(forName: appDomain)
{
for (key, value) in prefs where (key.hasPrefix(prefix) && (allowSet == nil || allowSet!.contains(key))) {
filteredPrefs[key] = value
}
}
return filteredPrefs
}
错误出现在这个特定的部分:
if let allowList {
allowSet = Set(allowList)
}
我从未使用过 if let
函数,所以我不知道如何解决这个 Swift 错误。有人能告诉我如何解决吗?
英文:
Firstly, I was using xcode version 13.2.1 and I have a problem when trying to build my ios App in terminal / xcode, here is the error response :
> /Users/user/.pubcache/hosted/pub.dev/shared_preferences_foundation-2.3.0/ios/Classes/SharedPreferencesPlugin.swift:58:22: Variable binding in a condition requires an initializer
and here is the full function that get error :
func getAllPrefs(prefix: String, allowList: [String]?) -> [String: Any] {
var filteredPrefs: [String: Any] = [:]
var allowSet: Set<String>?;
if let allowList {
allowSet = Set(allowList)
}
if let appDomain = Bundle.main.bundleIdentifier,
let prefs = UserDefaults.standard.persistentDomain(forName: appDomain)
{
for (key, value) in prefs where (key.hasPrefix(prefix) && (allowSet == nil || allowSet!.contains(key))) {
filteredPrefs[key] = value
}
}
return filteredPrefs
}
and the error comes from this specific function :
if let allowList {
allowSet = Set(allowList)
}
I never use if let function, so I don't know how to solve this swift error.
Can someone tell me how can I solved this?
答案1
得分: 1
显然,在较新的 Swift 版本中已经修复了这个问题,但在这里我会将 if
替换为:
allowSet = allowList == nil ? [] : Set(allowList!)
或者甚至更好的选项是:
var allowSet = Set<String>()
if allowList != nil {
Set(allowList!)
}
还有第三个选项:
var allowSet = allowList == nil ? Set<String>() : Set(allowList!)
英文:
Apparently this has been fixed in newer versions of swift but here I would replace the if
with
allowSet = allowList == nil ? [] : Set(allowList!)
or perhaps even better
var allowSet = Set<String>()
if allowList != nil {
Set(allowList!)
}
and a third option
var allowSet = allowList == nil ? Set<String>() : Set(allowList!)
答案2
得分: 0
你正在尝试使用“if let
缩写形式用于重叠现有可选变量”功能。这在Swift 5.7中实现。您正在使用的Xcode版本 - 13.2 - 仅支持Swift 5.5.2。请参见:https://swiftversion.net/
支持Swift 5.7的最低Xcode版本是14.0。您可以升级到该版本以修复错误。
您也可以重写代码。除了Joakim Danielson的建议之外,您还可以执行以下操作:
var allowSet = allowList.map(Set.init) ?? []
英文:
You are trying to use the feature "if let
shorthand for shadowing an existing optional variable". This is implemented in Swift 5.7. The Xcode version you are using - 13.2 - only supports Swift 5.5.2. See: https://swiftversion.net/
The lowest Xcode version that supports Swift 5.7 is 14.0. You can update to that version to fix the error.
You can also rewrite the code. In addition to Joakim Danielson's suggestions, you can also do:
var allowSet = allowList.map(Set.init) ?? []
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论