ios - Check if an element is present in 3D array Swift -
i have array gets instantiated in viewdidload
var bookingsarray : [[string]] = []
i adding elements in way:
var configuration: [string] = [] configuration.append(textfieldfacility.text!) configuration.append((pickslottf.text?.components(separatedby: " ")[0])!) configuration.append((pickslottf.text?.components(separatedby: " ")[1])!) bookingsarray.append(configuration [string])
bookingsarray
looks :
[["a", "20-08-2017", "14:00"], ["b", "20-08-2017", "14:00"]]
while adding new elements bookingsarray
, want check if new element present in array. how do in case of multi-dimensional array?
first of all, if want unique objects use set
.
if not possible highly recommend use custom struct can conform equatable
rather nested array , example
struct booking : equatable { let facilty : string let date : string let time : string static func ==(lhs : booking, rhs : booking) -> bool { return lhs.facilty == rhs.facilty && lhs.date == rhs.date && lhs.time == rhs.time } }
then declare array as
var bookingsarray = [booking]()
and create object
let datearray = pickslottf.text!.components(separatedby: " ") let configuration = booking(facility: textfieldfacility.text!, date: datearray[0], time = datearray[1]) bookingsarray.append(configuration)
the huge benefit can check
if bookingsarray.contains(item)
Comments
Post a Comment