0

Here is my code in Swift for an iOS app.

class Test {
    var name: String
    var sname: String

    init(name: String, sname: String) {
        self.name = name
        self.sname = sname
    }
}

class ArrTest {
    var arr = [Test]()
    init() {
        arr.append(Test(name: "test1", sname: "surname1"))
        arr.append(Test(name: "test2", sname: "surname2"))
        arr.append(Test(name: "test3", sname: "surname3"))
        arr.append(Test(name: "test4", sname: "surname4"))
    }

}

var x = ArrTest()
let obj = x.arr.filter { $0.name == "test1" }.first
println(obj?.sname)

I want to get the index and not the object of the first array object (in the array var). The "obj" x.arr.filter... returns the first object. I need the index of the correct object.

cateof
  • 6,608
  • 25
  • 79
  • 153
  • `let index = find(arr, Test("test1", "surname1"))!` – Kutyel Apr 24 '15 at 07:51
  • The `func find(includedElement: T -> Bool)` array extension given in this answer http://stackoverflow.com/a/24029119/1187415 to the duplicate question can be applied here. – Martin R Apr 24 '15 at 08:10
  • @Kutyel the find function does not work in playground. Do I need to include something? – cateof Apr 24 '15 at 08:36
  • No, there is no need to include anything, is part of the swift language, try changing your array syntax to this: `let arr = [ Test(name: "test1", sname: "surname1"), Test(name: "test2", sname: "surname2"), Test(name: "test3", sname: "surname3"), Test(name: "test4", sname: "surname4") ]` – Kutyel Apr 24 '15 at 08:44
  • @Kutyel I still get an error. Expression does not conform to type $T13 – cateof Apr 24 '15 at 08:58
  • @cateof: Did you have a look at the answer that I linked to in above comment? It should apply directly to your situation. – Martin R Apr 24 '15 at 09:10
  • @MartinR, yes I saw the answer at the link you posted and it works. However I was looking for a simpler/shorter answer in order to get the index of object. Your answer filters the array and then iterates with a for loop to find the index. I was looking for a one liner... – cateof Apr 24 '15 at 10:52
  • @cateof: It isn't my answer :) – And it does not filter the array. It *traverses* the array until a matching element is found, and then returns the index. There is no built-on solution for this. – Martin R Apr 24 '15 at 10:54
  • @MartinR, If I want the "one lines" I need the extension at the Array. – cateof Apr 24 '15 at 11:06
  • 1
    @cateof: I do not understand your remark. With the array extension of http://stackoverflow.com/a/24029119/1187415 you can write `let ind = x.arr.find( { $0.name == "test1" } )`, so it solves your problem. Without that array extension you cannot, because Swift does not provide such a method. – Martin R Apr 24 '15 at 11:34

1 Answers1

0

Well you can simply use the method find of the array:

// This is pseudocode haven't tried it but should go along those lines.
var x = ArrTest()
var searchMe = Test("test1", "surname1");
let obj = find(x, searchMe);

For this to work you need to implement the Equatable protocol, here you can find a simple tutorial. After that find should work with your test class.

let arr:Array = ["a","b","c"]
find(arr, "c")!              // 2
find(arr, "d")   
Jose Luis
  • 3,307
  • 3
  • 36
  • 53