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

标签 swift swift2

我正在将我的项目从 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/

相关文章:

ios - 未调用手势识别器

ios - 如何隐藏下一屏的过渡searchBar

swift - &/and &% 从 Swift 语言中移除了吗?

swift - 如何在程序中打印出 Swift 的版本?

ios - RevealViewController 中的 didSelectRowAtIndexPath

ios - 在 TabBarController 中添加 View Controller 作为带有导航栏的 subview

ios - 从 API 使用 Swift 动态填充 iOS TableView

objective-c - 使用 Objective-C 宏访问 Swift 中的 UIView 帧测量

ios - Prototype Cell 中的 UISwitch,多行仅引用最后一行。为什么

ios - 特殊阴影 : Swift 2