0

I am trying to access an array in a function, as far as I understand there is no Arraylists in Swift, so I am trying to use regular list:

  func findNearsetPoints(pointsArray: [Point] , myPoint: Point )-> Array <Point>{

    var twoPoint = [Point]()
    var minDist1:Double = DBL_MAX;
    var minDist2:Double = DBL_MAX;
    var distance:Double = 0

    for element in pointsArray{
        distance = getDistanceBetweenTwoPoints(pointsArray[element], Point); //error 0
        if (distance < minDist1) {
            minDist1 = distance;
            twoPoint[1] = twoPoint[0];
            twoPoint[0] = pointsArray[element]; // error 1
        }
        else if (distance < minDist2) {
            minDist2 = distance;
            twoPoint[1] = pointsArray[element]; //error 1
        }
    }

    return twoPoint;
    }

    func getDistanceBetweenTwoPoints(point1:Point , point2:Point )->Double{
        return 5; //TODO
    }

error 0:

/Users/user/Desktop/proj/ViewController.swift:145:38: Cannot subscript a value of type '[ViewController.Point]' with an index of type 'ViewController.Point'

error 1:

/Users/user/Desktop/proj/ViewController.swift:149:38: Cannot subscript a value of type '[ViewController.Point]' with an index of type 'ViewController.Point'

What is wrong with the code? thanks!

Coder123
  • 784
  • 2
  • 8
  • 29

2 Answers2

1

your element is already a Point and not an index.

for element in pointsArray{
    distance = getDistanceBetweenTwoPoints(point1: element, point2: myPoint) // fix here
    if (distance < minDist1) {
        minDist1 = distance;
        twoPoint[1] = twoPoint[0];
        twoPoint[0] = element; // fix here
    }
    else if (distance < minDist2) {
        minDist2 = distance;
        twoPoint[1] = element; // fix here
    }
}

PS: take a loook also to this question "Sort array by calculated distance in Swift" for better calculation of distance. just sort the array by distance and then take the first from the array after it is sorted. thats more easy to do

Community
  • 1
  • 1
muescha
  • 1,544
  • 2
  • 12
  • 22
  • That was a big help! now its yelling in the first line /Users/user/Desktop/proj/PdacJobTest/ViewController.swift:137:47: Missing argument labels 'point1:point2' in call – Coder123 Jan 22 '17 at 16:51
  • then you not call it right it should be somehow: `let twopoints = findNearsetPoints(pointsArray: myPointsArray , myPoint: myPoint )` – muescha Jan 22 '17 at 16:58
  • this is the problematic line: distance = getDistanceBetweenTwoPoints(element, Point); – Coder123 Jan 22 '17 at 17:02
  • 1
    when you add labels you need to use the labels or mark the label with an `_` that you don't need the labels. call it: `distance = getDistanceBetweenTwoPoints(point1: element, point2: myPoint);` and change it to `myPoint` instead of the class Name – muescha Jan 22 '17 at 17:07
  • just a note: you don'd need a `;` at the end of line in swift – muescha Jan 22 '17 at 17:08
1

You declared getDistanceBetweenTwoPoints as taking named parameters.

With the current declaration, you need to call it using this syntax:

getDistanceBetweenTwoPoints(point1: aPoint, point2: anotherPoint)

If you want to call it without labels on the parameters then you should redefine it like this:

func getDistanceBetweenTwoPoints(
  _ point1:Point , 
  _ point2:Point )->Double{
        return 5; //TODO
    }
Duncan C
  • 128,072
  • 22
  • 173
  • 272