gpt4 book ai didi

swift - 迁移到 Swift 2 "Use of unresolved identifier ' ** *'"

转载 作者:行者123 更新时间:2023-11-30 10:09:12 24 4
gpt4 key购买 nike

我正在将我的项目从 Swift 1.2 升级到 Swift 2。

我利用这个机会升级了我使用的许多库,特别是 Alamofire。

但是现在我的许多请求都遇到了这个错误:

Use of unresolved identifier 'notifTypeJSON'

这是其中一个函数的代码:

func getNotifications(failure failure: (NSError) -> (), success: ([Notification]) -> ()) {
Alamofire.request(Router.Notifications)
.validate()
.responseJSON { response in
if let error = response.result.error {
failure(error)
} else {
var json = JSON(response.data!)
let status = json["error"].intValue
if status != 0 {
failure(self.createError(status))
} else {
var notifications = [Notification]()
let notificationsList = json["notification"]
for (index: String, notifTypeJSON: JSON) in notificationsList {
if let notifJSON = notifTypeJSON[NotificationTypes.Generic.rawValue].dictionaryObject {
notifications.append(GenericNotification(json: notifJSON))
}
else if let notifJSON = notifTypeJSON[NotificationTypes.Follow.rawValue].dictionaryObject {
notifications.append(FollowNotification(json: notifJSON))
}
else if let notifJSON = notifTypeJSON[NotificationTypes.Comment.rawValue].dictionaryObject {
notifications.append(CommentNotification(json: notifJSON))
}
else if let notifJSON = notifTypeJSON[NotificationTypes.Like.rawValue].dictionaryObject {
notifications.append(LikeNotification(json: notifJSON))
}
}
DDLogInfo("SeetyAPI getNotifications() success")
success(notifications)
}
}
}
}

最佳答案

在 Swift 2 中,我们用元组循环字典的方式已经改变:类型必须位于单独的元组中。

之前的示例:

for (key:String, value:Int) in xxx {

之后的示例:

for (key, value):(String, Int) in xxx {

所以对于你来说,你需要替换它:

for (index: String, notifTypeJSON: JSON) in notificationsList {

这样:

for (index, notifTypeJSON):(String, JSON) in notificationsList {

关于swift - 迁移到 Swift 2 "Use of unresolved identifier ' ** *'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com