斯威夫特 - 如何获取一个具有初始化数组的索引? [英] Swift - How to get the index of an array that has an initializer?

查看:156
本文介绍了斯威夫特 - 如何获取一个具有初始化数组的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个IOS应用程序,我用快捷。我有一个数组,我需要为了从我的CoreData数据库中检索数据。

I am making an IOS app and I use swift. I have an array that I need in order to retrieve data from my CoreData database.

 var myList : Array<AnyObject> = []

这myList中数组有所谓的星期一的项目。我想获得这个项目的索引。我试图用这个来获得指标,但它不能正常工作。

This "myList" array has an item called "Monday". I want to get the index of this item. I tried to use this to get the index, but it doesn't work.

find(daysOfWeek, "c")!

它给了我一个错误的遗传Paramater不能绑定到非@对象协议AnyObject''

发现方式工作正常,正常的数组是这样的:

This find method works fine for normal arrays like this:

var daysOfWeek = ["Monday", "Tuesday"]

我怎样才能得到一个项目的索引我myList中数组?

How can I get the index of an item for my myList array?

推荐答案

由于错误状态,找到不能 AnyObject ;所以虽然这不会工作:

As the error states, find can't work with AnyObject; so although this won't work:

var myList : Array<AnyObject> = []

find(myList, "c")

这将:

var myList : Array<String> = []

find(myList, "c")

既然你要搜索的字符串,使 myList中成数组字符串应该足够了。

这篇关于斯威夫特 - 如何获取一个具有初始化数组的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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