Swift creates a reference to a mutable array

Refresh Swift arrays are too weird. I think it is more reliable to use NSMutableArrayinstead if necessary.

I understand that Swift usually passes an array by value, not by reference, but I need to be able to pass the link in code as follows:

var arr1 = [1, 2, 4]
var arr2 = [4, 6, 3]
var result = pickOne() ? arr1 : arr2
result[2] = 7
// one of these should be updated, currently they are just as initialized
println(arr1)
println(arr2)

I want to dynamically select one of two arrays and make changes to this array. Both arrays have the same size and type of content, and I would only change the elements, not add or delete. How can I achieve this?

Edit: is there an answer instead instead NSMutableArray?

Just to clarify: pickOne()- this is a function that I already have, and based on the results of this function, I want to change the first or second array. Therefore, I do not need help in choosing what to choose. I need help on how to make changes (and there will be several) to this desired array without having to check which array to use for each modification.

I could do something like this:

if pickOne() {
    // work on arr1
    // ...
} else {
    // work on arr2
    // ...
}
// do some other work
// ...
if pickOne() {
    // work more on arr1
    // ...
} else {
    // work more on arr2
    // ...
}
// do yet another set of things
// ...
if pickOne() {
    // work again on arr1
    // ...
} else {
    // work again on arr2
    // ...
}

But it gets old quickly.

Edit So far, I have made some progress here.

var result = pickOne() ? UnsafeMutableBufferPointer(start: &arr1, count: arr1.count) : UnsafeMutableBufferPointer(start: &arr2, count: arr2.count)

, , "", , . , Unsafe - .

+4
1

Swift Array, . :

" , Swifts String, Array Dictionary . , , , .

NSString, NSArray NSDictionary Foundation, , . NSString, NSArray NSDictionary , "

: Apple Inc. " ". . https://itun.es/ca/jEUH0.l

+1

Source: https://habr.com/ru/post/1589504/


All Articles