0

In my swift ios app I have a structure:

class SingleStructure: NSObject {

    let id: String
    let body: String
    var sent_at: NSDate
    var requiredId: String

}

I declared a list of those structures:

var singleStructuresList: [SingleStructure] = []

I also have a String file:

var someString: String = "hello"

and I want to check if singleStructuresList contains a SingleStructure that has a requiredId equal to my someString. How can I do it?

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • What did you try and what is the problem you faced? – FredericP Apr 27 '16 at 19:17
  • First I operated on only `string` values - so a simple `.contains` method was enough, now I had to switch to the structures and I ok, I could do a `for` loop and look for it manually, but I suspect there might be a more practical solution. Since I don't have a big experience with Swift I've decided to ask here – user3766930 Apr 27 '16 at 19:19
  • Do you have lots of SingleStructures in your table? A for loop can be good solution: simple and easy to understand! – FredericP Apr 27 '16 at 19:31
  • yes I do, actually it depends, mostly quite a few. I tried the solution proposed by Martin R for using extensions and I pasted `extension SequenceType where Generator.Element : Equatable { func contains(element: Self.Generator.Element) -> Bool }` but I'm getting error on the `func...` line saying that `Expected { in body of function declaration` – user3766930 Apr 27 '16 at 19:48
  • You can try this one (tested in playground: search for a "T" `let myarray = ["noop", "Test", "anoTher"] var result = myarray.map{ $0.containsString("T") }` but you still have to iterrate result to search for true – FredericP Apr 27 '16 at 20:29
  • Thanks! So what exactly do you mean by iterating result? Will that `myarray.map` return some kind of list of boolean values? – user3766930 Apr 27 '16 at 20:50
  • Yes, you can try it in a Playground: result is an array of boolean (.map always return an array) – FredericP Apr 27 '16 at 21:00

0 Answers0