15

I'm getting extraneous argument label 'contentsOf:' in call array.append(contentsOf: test) error when trying to run this code in playground:

import Cocoa

var array:[Any] = []
let test = [""]
array.append(contentsOf: [""])
array.append(contentsOf: test)

Why is this happening? As I understand, there are two equal arrays with empty string.

Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
Stanislav Chernischuk
  • 975
  • 1
  • 11
  • 24
  • just declare test as `let test:[Any] = [""]` and it will stop complaining. Another option is to change the array type `var array:[String] = []` – Leo Dabus Apr 18 '17 at 23:15
  • 1
    Compare http://stackoverflow.com/q/41096163/2976878 – Hamish Apr 18 '17 at 23:18
  • `test` (which has type `[String]`) has to be converted to `[Any]` before it can be appended. This process requires each constituent `String` be boxed into a protocol witness table. Since it's an `O(n)` operation, it's probably best that the compiler doesn't silently do this without informing you that such a conversion is happening. – Alexander Apr 18 '17 at 23:36
  • Check if you have any typos or confused case-insensitivity variables somewhere in the array – onmyway133 Aug 27 '18 at 08:58

1 Answers1

22

To answer your specific question in the comments, in that case you just need to cast so Swift knows you're aware. In this case since SKShapeNode downcasts to SKNode just fine, you can just cast with as. If you were doing a cast that may fail, you'd need to use as? and safely unwrap to be sure.

var allNodes: [SKNode] = []
let onlyShapeNodes: [SKShapeNode] = []

allNodes.append(contentsOf: onlyShapeNodes as [SKNode])

For the original generic example this would work as well.

var array: [Any] = []
let test = ["", ""]

array.append(contentsOf: [""] as [Any])
array.append(contentsOf: test as [Any])
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
  • Thank you. I understand. I mistakenly thought that since SKShapeNode is inherited from SKNode, I can easily add an array of SKShapeNodes to the SKNodes array without any "as" usage. Especially I was confused by the fact that I still can write something like ```array.append (SKShapeNode ())```, where ```array``` is ```[SKNode]```- and add one SKShapeNode with no errors – Stanislav Chernischuk Apr 18 '17 at 23:30
  • Yea in some cases the compiler can infer what you're trying to do such as `array.append(SKShapeNode())` where others it can't because the type is explicit. This happens a lot with numbers too. `1 + 5.0` works but if you put them into variables and did `valueInt + valueDouble` it would get mad. Because the types are saved at that point. – Ryan Poolos Apr 18 '17 at 23:37
  • Anyway, this is very strange. Just look at my original question code. Now I don't understand other thing. If we need to cast ```test``` to ```[Any]```, why we don't need to cast ```[""]``` to ```Any``` like this: ```array.append(contentsOf: [""] as [Any])```? I saw that it was in your answer. But in the first case everything works without ```as [Any]``` – Stanislav Chernischuk Apr 18 '17 at 23:46
  • In other words: append ```test``` - error. Append ```test as [Any]``` - ok. But append ```[""]``` - ok, and append ```[""] as [Any]``` is ok too... – Stanislav Chernischuk Apr 18 '17 at 23:55
  • Because similar to putting `SKShapeNode()` directly in it can infer the type of `[""]` if its passed directly in. – Ryan Poolos Apr 19 '17 at 14:05