1

Does anyone have an idea how to write the following code in a shortened form?

if self.atPoint(locationUser) == blueMarkArray[0] || self.atPoint(locationUser) == blueMarkArray[1] || self.atPoint(locationUser) == blueMarkArray[2] || self.atPoint(locationUser) == blueMarkArray[3] || self.atPoint(locationUser) == blueMarkArray[4] || self.atPoint(locationUser) == blueMarkArray[5] || self.atPoint(locationUser) == blueMarkArray[6]{

            print("its here")
        }

Furthermore, I don't want to commit myself to a fixed number of elements in the blueMarkArray array. It should be as variable as possible, because for example it can contain 4 elements, but also 12 elements.

For each answer I am very grateful.

EDIT:

How can you access the found element if I want to write the following: someNode.position = elementOfMarkArray.position

TimKrs
  • 67
  • 1
  • 9
  • This is probably not a duplicate as I don't think you want to be testing if a point is in the list in that manner, due to floating point precision issues. You probably want to test if the point is *near* one of these points by calculating their distance. – trojanfoe Feb 11 '19 at 10:28
  • Your added question is answered here: https://stackoverflow.com/a/24069331. – Martin R Feb 11 '19 at 10:32
  • @MartinR I believe you are wrong marking this question as just another *find-in-list* issue. Please vote to re-open. – trojanfoe Feb 11 '19 at 10:34
  • @trojanfoe: As I understand it, OP asks how to shorten the comparison against multiple array elements, or how to find its position in an array. That has been asked and answered before. – It *might* be better to find the “nearest” array element, but that is not what is asked here. – Martin R Feb 11 '19 at 10:37
  • @MartinR Agreed the OP has not asked the question correctly; as well as being unaware of the nuisances of collection classes etc, they appear to lack understanding of 2D graphics. – trojanfoe Feb 11 '19 at 10:38
  • @trojanfoe: Haven't seen you here for a while – welcome back! – Martin R Feb 11 '19 at 10:39
  • @MartinR Yeah it's been a while. Many thanks! – trojanfoe Feb 11 '19 at 10:58

3 Answers3

3

Use array's contains method

if (blueMarkArray.contains(self.atPoint(locationUser))) {
   //
}

or, if you need to check only up to index 6 like in your example, use

if (blueMarkArray[0...6].contains(self.atPoint(locationUser))) {
   //
}

if you want to get the index of the element, you can use firstIndex of lastIndex methods

if let index = blueMarkArray.firstIndex(of: self.atPoint(locationUser)) {
   //
}
mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • It works, but how can you access the found element if I want to write the following: `someNode.position = elementOfMarkArray.position` – TimKrs Feb 11 '19 at 10:24
  • See updated answer – mag_zbc Feb 11 '19 at 10:27
  • Your first and second example are the same, the second doesn't actually check only up to index 6. There's also no need to use parentheses for the condition of an `if` statement in Swift, unlike in Objective-C (and the common pattern is not to use them). – Dávid Pásztor Feb 11 '19 at 10:29
  • @DávidPásztor an error during edit, now corrected – mag_zbc Feb 11 '19 at 10:30
  • Unfortunately, I still don't quite understand how to use `index`. Unfortunately it doesn't work that way: `if let index = blueMarkArray.firstIndex(of: self.atPoint(locationUser) as! SKSpriteNode) { someNode.position = index.position }` – TimKrs Feb 11 '19 at 10:35
  • I got it and it works. Thanks a lot – TimKrs Feb 11 '19 at 10:45
1

You should simply use the contains method of Array, which will return true if any of the elements of the array equals self.atPoint(locationUser).

if blueMarkArray.contains(self.atPoint(locationUser)) {
    print("it's here")
}

If you also need to access the element that matches the object/value you are looking for, you can use first(where:) or firstIndex(of:).

if let location = blueMarkArray.first(where: {$0 == self.atPoint(locationUser)}) {
    someNode.position = location.position
}

or

if let locationIndex = blueMarkArray.firstIndex(of: self.atPoint(locationUser)) {
   let location = blueMarkArray[locationIndex]
   someNode.position = location.position
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
1

You can use the array's method contains:

if blueMarkArray.contains(self.atPoint(locationUser)) {
    print("its here")
}
Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37