Array –join(_:)
function throws an EXC_BAD_ACCESS.
var ar1 = [1,2,3]
var ar2 = [5,6,7]
var res = ar1.join(ar2)
Has anyone faced this problem? Any solution or suggestion?
var Array1 = ["Item 1", "Item 2"] var Array2 = ["Thing 1", "Thing 2"] var Array3 = Array1 + Array2 // Array 3 will just be them combined :) Save this answer.
Mutable CollectionsIf you declare an array using the var keyword, the array is mutable. This means that you can add and remove elements from the array. In this example, we declare an empty array, colors . The array is mutable because we use the var keyword to declare it.
An array is a list of values. In Swift, arrays are typed. You declare the type of the array (or Swift infers it) and you can only add items that are of the same type to the array. You can't generally store multiple different types in an array.
What you want is
var ar1 = [1,2,3]
var ar2 = [5,6,7]
var res = ar1 + ar2
You would usually use join() to flatten a two level array by inserting the elements from another array in between first level elements:
var ar1 = [1,2,3]
var ar2 = [[4,5,6],[7,8,9],[10,11,12]]
let res = ar1.join(ar2) // [4, 5, 6, 1, 2, 3, 7, 8, 9, 1, 2, 3, 10, 11, 12]
The function works in the same fashion for strings also:
let ar1 = ["1","2","3"]
let res = ".!?".join(ar1) // "1.!?2.!?3"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With