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!

2
  • You want to use pointsArray.enumerated() to get a tuple of index, value Commented Jan 22, 2017 at 16:50
  • thats right, but for his use case he don't need the index Commented Jan 22, 2017 at 16:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

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
then you not call it right it should be somehow: let twopoints = findNearsetPoints(pointsArray: myPointsArray , myPoint: myPoint )
this is the problematic line: distance = getDistanceBetweenTwoPoints(element, Point);
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
just a note: you don'd need a ; at the end of line in swift
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
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.