Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "extraneous argument label" when trying to append array to other array in Swift

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.

like image 784
Stanislav Chernischuk Avatar asked Apr 18 '17 23:04

Stanislav Chernischuk


1 Answers

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])
like image 163
Ryan Poolos Avatar answered Sep 20 '22 22:09

Ryan Poolos