0

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> = []

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")!

It gives me an error 'Genetic Paramater cannot be bound to non-@object protocol 'AnyObject''

This find method works fine for normal arrays like this:

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

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

3 Answers 3

1

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

var myList : Array<AnyObject> = []

find(myList, "c")

this will:

var myList : Array<String> = []

find(myList, "c")

Since you're searching for a string, making myList into an array of Strings should be sufficient.

Sign up to request clarification or add additional context in comments.

Comments

0
var shoppingList:Array = ["Chocolate Spread", "Cheese", "Butter"]

var indexOfObject = 0

for var index = 0; index < shoppingList.count ; index++ {
if "Butter" == shoppingList[index] {
    indexOfObject = index
}
}

println("Butter index is \(indexOfObject)")

I hope this ll help you to find the index of the object.. I don't there is any function to get index directly bt now i use like above only..

1 Comment

Yes it works with a simple array but I have a different array, it's an anyobject array: var myList : Array<AnyObject> = []
0

In any case, find requires a type that conforms to Equatable (i.e. has ==). == is not defined on AnyObject (there is no general definition of value equality on objects). However, == is defined on NSObject:

import Foundation
var myList : [NSObject] = []
find(myList, "c")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.