Swift: Determine if array contains array [duplicate] - ios

This question already has answers here:
Why can't I check to see if my array of arrays contains a specific array?
(2 answers)
Closed 7 years ago.
Im trying to figure out how to check if array contains another array:
var grid: [[Int]]!
grid = []
grid.append([1,1])
if grid.contains([1,1]) {
}
but the line
if grid.contains([1,1]) {
throws the error:
Contextual type '#noescape ([Int]) throws -> Bool' cannot be used with
array literal

Swift arrays doesn't conform to Equatable by default. But you can still compare them in the predicate:
if (grid.contains { $0 == [1,1] } == true) {
}

Related

Is it possible to use Contains on an array of objects using one equatable object field? [duplicate]

This question already has an answer here:
Swift - Check if array contains element with property [duplicate]
(1 answer)
Closed 2 years ago.
Giving the following object as an example, is it possible to check if an array of this object contains specific numeric number based on the "number" field alone using the array ".contains" method ?
struct AvailableDay {
var someField : someObject
var number : Int
}
The array:
var availableDay : [AvailableDay]
availableDay.contains(...) /*Just to absolutely clarify what I meant by "contains".*/
Use contains(where:
availableDay.contains(where: { $0.number == yourDesiredNumber})
Alternatively, you can override the equitable conformance to check only for the number property, like this:
extension AvailableDay: Equatable {
static func == (lhs: AvailableDay, rhs: AvailableDay) -> Bool { lhs.number == rhs.number }
}

Testing if array contains at least one object that has some property [duplicate]

This question already has answers here:
Shorthand to test if an object exists in an array for Swift?
(2 answers)
Closed 4 years ago.
I want to test if the given array contains at least one object that contains a specific 'string' in it. Is it useful and possible?
Try filter().
struct S { let string: String }
let array = [ S(string: "a"), S(string: "b") ]
let hasAtleastOneA = array.filter({ $0.string == "a" }).count > 0
something like this :
let array = ["a","b","c"]
if array.count > 0 {
for name in array {
if name.contains("a"){
print("YES")
}
}
}
You check do this way,
let filtered = data.filter({$0.contains("test")})
Reference Higher order functions in swift: Filter, Map, Reduce, flatmap

Check if an object is the last in the array [duplicate]

This question already has answers here:
Cannot convert value of type 'Meme!' to expected argument type '#noescape (Meme) throws -> Bool'
(2 answers)
Closed 6 years ago.
I have an array of [myObject] called myArray and an myObject object.
I want to check if myObject is the last object in myArray.
What is the best way to do it?
I've tried:
if myObject == myArray.last!
but it gives me this error:
Binary operator '==' cannot be applied to two 'myObject' operands
Any idea?
Thanks!
Try this one may be work for you.
let ary = ["1","2","3"]
if ary[0].isEqual(ary.last!){
print("Is last object")
}else{
print("Is Not last object")
}
Your output should be:Is Not last object
let ary = ["1"]
if ary[0].isEqual(ary.last!){
print("Is last object")
}else{
print("Is Not last object")
}
Your output should be:Is last object

How to find index of Int element in an array? [duplicate]

This question already has answers here:
How to find index of list item in Swift?
(23 answers)
Closed 7 years ago.
Is there a "better"(less verbose) way of doing this?
var found: Int?
for i in 0...myArray-1 {
if myArray[i] == 3 {
found = i
}
}
similar to
let i = find(myArray, "1")
but for Int ?
This question is slightly "different" than How to find index of list item in Apple's swift? because it directly address the find function for Int, instead of String. Granted, the only difference is the title and pointing out that you can remove the quotation marks to get Int.
Here's an Int example
let myArray = [1, 2, 3]
var i = find(myArray, 2)
Good luck

Issue with calling function in swift [duplicate]

This question already has answers here:
Why does a function call require the parameter name in Swift?
(6 answers)
Closed 8 years ago.
I was following optional values tutorial, in which we have following method
func findIndexOfString (string : String, array : String[]) -> Int?{
for (index, value) in enumerate(array){
if(value == string){
return index
}
}
return nil
}
however if i call this method by
let indexFound = findIndexOfString("myString", neighbour) //neighbour is array of String
give error that "Missing argument label ''array" in call , means i have to call this by
let indexFound = findIndexOfString("myString", array:neighbour)
Is it made compulsory to mention argument label in call?
Yeah. It is compulsory for for class methods. You should use the parameter names except for the first parameter. There comes the difference between the class methods and functions, for functions you will not use(You cant unless function defines an external parameter name) the parameter names.

Resources