6

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

Community
  • 1
  • 1
Kostiantyn Koval
  • 8,407
  • 1
  • 45
  • 58
  • join is not the operation to add , http://stackoverflow.com/questions/24002733/add-an-element-to-an-array – Arun Jul 18 '14 at 08:32
  • 1
    so what's thy porpoise of this method? It takes Sequence and return and Array [T]. – Kostiantyn Koval Jul 18 '14 at 08:43
  • The join method for strings in objective is perhaps better named: `componentsJoinedByString` but is not available for arrays. – zaph Jul 18 '14 at 13:39

1 Answers1

6

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"
carlossless
  • 1,171
  • 8
  • 23