'AnyObject'没有会员“联系”尽管Intellitype说它有吗?

时间:2015-03-01 00:31:55

标签: ios arrays json xcode swift

另一个小时Swift编程问题。

我一直在从对象“Any Object”“Results”的数组中返回一个对象的值。 intellitype说我在数组中有一个值为“ContactUID”的对象但是当我尝试使用ContactUID时,我得到一个错误,说'AnyObject'不包含成员'contactUID'。

名为HBCContactList的Array成功返回,FirstName,LastName以及代码中屏幕上列出的所有其他项。但是它不会返回值'ContactUID'。

模特得到了正确的项目。然而不像所有其他... ContactUID是一个INT64而不是一个字符串......我添加了一些截图来协助解释过程。对不起,这听起来很复杂,但我希望我错过了一些愚蠢的东西。

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

iOS上的自动填充并不总是准确的,通常它会列出所有可能的选择器/方法。

你问题的根源在于即使你知道HCCContactList只保存HBCDirectoryModel对象,编译器也不会像MOContext.executeFetchRequest(freq, error: nil)返回一个声明它包含AnyObject&#39;的数组。 s([AnyObject] / Array<AnyObject>)。要将这些对象中的任何一个引用为HBCDirectoryModel,您需要对此类型进行转换。

最简单的方法是将HCCContactList声明为HBCDirectoryModel而不是AnyObject的数组,然后再投射将MOContext.executeFetchRequest()调用到同一类型的结果。

您可以按照以下方式执行此操作

var HCCContactList: Array<HBCDirectoryModel> = []
HCCContactList = MOContext.executeFetchRequest(freq, error: nil) as Array<HBCDirectoryModel>

或使用较短的语法

var HCCContactList:[HBCDirectoryModel] = []
HCCContactList = MOContext.executeFetchRequest(freq, error: nil) as [HBCDirectoryModel]