'INT'是无法转换为“范围<&诠释GT; [英] 'Int' is not convertible to 'Range<Int>

查看:134
本文介绍了'INT'是无法转换为“范围<&诠释GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PFObjects 的数组,我创建了一个新的数组: restaurantNames 来存储所有他们的名字。现在,我想打印UIPageView的名字,但我得到的错误:

I have an array of PFObjects and I've created a new array: restaurantNames to store all their names. Now I'm trying to print the names on UIPageView, but I get the error:

'Int' is not convertible to 'Range<Int>'

code:

private func getItemController(itemIndex: Int) -> PageItemController? {

    let restaurantNames = finalRestaurantArray.map { $0.objectForKey("Name") }

    if itemIndex < finalRestaurantArray.count {
        let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as PageItemController
        pageItemController.itemIndex = itemIndex
        pageItemController.restaurantName = restaurantNames[itemIndex]
        //pageItemController.imageName = finalRestaurantArray[itemIndex]
        return pageItemController
    }

    return nil
}

实际PFObject阵列看起来像这样( restaurantNames 只包含名称的数组):

[<Restaurant: 0x7feb9252cfc0, objectId: LA74J92QDA, localId: (null)> {
    Name = "Restaurant 1";        
    City = "New York";
    Closed = 1;
    Country = "United States";
    FoodType = Japanese;
}, <Restaurant: 0x7feb926afa00, objectId: 0aKFrpKN46, localId: (null)> {
    Name = "Restaurant 2";        
    City = "San Francisco";
    Closed = 1;
    Country = "United States";
    FoodType = Italian;
}]

引擎收录: http://pastebin.com/U63TFMRV

推荐答案

这行是混乱的编译器:

let restaurantNames = finalRestaurantArray.map { $0.objectForKey("Name") }

这将创建一个 [AnyObject?] 这是一个非常困难的编译器来处理,你会得到经常混淆的错误(因为各种促销活动和过载)。

This creates an [AnyObject?] which is a notoriously difficult for the compiler to deal with, and the errors you get are often confusing (because of various promotions and overloads).

第1步是,你应该避免 AnyObject 只要有可能。因此, finalRestaurantArray 应该是实际的对象数组,而不是 [AnyObject] ,甚至字典。那么你的地图将返回 [字符串] 就像我相信你想让它。

Step 1 is that you should avoid AnyObject whenever possible. So finalRestaurantArray should be an array of the actual objects rather than [AnyObject], or even dictionaries. Then your map would return a [String] like I believe you want it to.

您的可以的攻入这个地方是这样的:

You can hack this into place like this:

// Avoid this
let restaurantNames = finalRestaurantArray.map { $0.objectForKey("Name") as String }

如果有什么差错,它会崩溃。不要那样做。让您的数据移出的 AnyObject 一样快,你可以​​(把它弄出来词典的一样快,你可以​​,太),并把它变成可以保证他们会返回一个值,真正的模型对象。

And if anything goes wrong, it'll crash. Don't do that. Get your data out of AnyObject as fast as you can (and get it out of Dictionary as fast as you can, too), and turn it into real model objects that can guarantee they'll return a value.

这篇关于'INT'是无法转换为“范围&LT;&诠释GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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