模糊地使用下标xcode 7.1 [英] Ambiguous use of subscript xcode 7.1

查看:135
本文介绍了模糊地使用下标xcode 7.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

var jsonResult = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
var count = jsonResult["levels"]!.count as Int
for var i=0; i<count; ++i {
   let obj = jsonResult["levels"]![i] as! NSDictionary
   ...
}

在最后一行我收到这个错误:

On the last line I am receiving this error:


模糊地使用下标

Ambiguous use of subscript

我该如何解决这个问题?

How can I resolve this?

此代码已经工作了一段时间,但随着xcode 7.1的升级,它破坏并停止工作。

This code has worked for some time but with the upgrade to xcode 7.1 it broke and stopped working.

推荐答案

你必须告诉编译器中间对象在行中是什么

You have to tell the compiler what the intermediary object is in the line

let obj = jsonResult["levels"]![i] as! NSDictionary

语句 jsonResult [levels]之后!编译器不知道他正在处理什么样的对象。你必须告诉它是 NSArray 或其他:

After the statement jsonResult["levels"]! the compiler does not know what kind of object he is dealing with. You have to tell it that is an NSArray or something else:

let obj = (jsonResult["levels"] as! NSArray)[i] as! NSDictionary

当然你应该另外确保你可以实际做所有的铸造和内部的对象json实际上是预期的类型。

Of course you should additionally make sure that you can actually do all that casting and that the objects inside the json are really of the expected type.

通过直接转换为数组只使用一个强制转换,甚至更短一点of NSDictionary

Even a little bit shorter using only one cast by directly casting to an array of NSDictionary:

let obj = (jsonResult["levels"] as! [NSDictionary])[i]

推理保持不变:你告诉编译器什么键入 jsonResult [levels] 。它应该是一个包含 NSDictionary s的数组。

The reasoning remains the same: you tell the compiler of what type jsonResult["levels"]. It is supposed to be an array containing NSDictionarys.

这篇关于模糊地使用下标xcode 7.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆