Skip to main content
deleted 24 characters in body
Source Link
Coder123
  • 864
  • 2
  • 10
  • 32

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/PdacJobTest/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/PdacJobTest/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!

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/PdacJobTest/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/PdacJobTest/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!

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!

removed tag from title
Link
dandan78
  • 14.1k
  • 13
  • 68
  • 82

Access in array in specific indices [Swift]

added 4 characters in body
Source Link
vadian
  • 286.2k
  • 32
  • 378
  • 379

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/PdacJobTest/ViewController.swift:145:38: Cannot subscript a value of type '[ViewController.Point]' with an index of type 'ViewController.Point'

/Users/user/Desktop/proj/PdacJobTest/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/PdacJobTest/ViewController.swift:149:38: Cannot subscript a value of type '[ViewController.Point]' with an index of type 'ViewController.Point'

/Users/user/Desktop/proj/PdacJobTest/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!

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/PdacJobTest/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/PdacJobTest/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!

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/PdacJobTest/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/PdacJobTest/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!

Source Link
Coder123
  • 864
  • 2
  • 10
  • 32
Loading