Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Array join EXC_BAD_ACCESS

Tags:

arrays

join

swift

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?

enter image description here

like image 573
Kostiantyn Koval Avatar asked Jul 18 '14 08:07

Kostiantyn Koval


People also ask

How do I append two arrays in Swift?

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.

How do you declare a mutable array in Swift?

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.

How do arrays work in Swift?

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.


1 Answers

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"
like image 193
carlossless Avatar answered Nov 02 '22 02:11

carlossless