1

I'm creating Native Module for React Native. I'm trying to process the data in Swift, here's the function

enter image description here

func getCornerPoints(_ cornerPoints: [AnyHashable]?) -> [AnyHashable]? {
    var result: [AnyHashable] = []
    
    if cornerPoints == nil {
      return result
    }
    for point in cornerPoints ?? [] {
      guard let point = point as? NSValue else {
        continue
      }
      var resultPoint: [AnyHashable: Any] = [:]
      resultPoint["x"] = NSNumber(value: Float(point.cgPointValue.x))
      resultPoint["y"] = NSNumber(value: Float(point.cgPointValue.y))
      
      result.append(resultPoint) // error is here "No exact matches in call to instance method 'append'"
    }
    return result
  }
dimasjt
  • 170
  • 4
  • 8
  • @vadian okay. it's answer my question to change `Any` to `NSNumber` but, how if the value is not only `NSNumber` ```swift var linesElements: [AnyHashable] = [] for line in lines { var l: [AnyHashable: Any] = [:] l["text"] = line.text l["cornerPoints"] = getCornerPoints(line.cornerPoints) linesElements.append(l) } ``` – dimasjt Oct 30 '21 at 01:31

1 Answers1

2

Any does not conform to Hashable therefore [AnyHashable: Any] (variable resultPoint) cannot be represented by AnyHashable.

But why is the value of resultPoint Any at all? It's clearly NSNumber. If you declare resultPoint

var resultPoint: [AnyHashable: NSNumber] = [:]

then the code compiles.

And why is the return type optional? result is – also clearly – non-optional

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Note that [Swift implicitly converts to `AnyHashable`](https://stackoverflow.com/questions/42021207/how-are-int-and-string-accepted-as-anyhashable). So it doesn't matter that “`[AnyHashable: Any]` is not equal to `AnyHashable`”. It only matters that `[AnyHashable: Any]` does not **conform** to `Hashable` (because `Any` does not conform to `Hashable`). – rob mayoff Oct 29 '21 at 17:02