How to pass array by reference without function ? swift

david

I have this class:

class MainView:UIView{

    var categories:[Category]!

}

i want to set the categories arg, but i need to pass it by reference not value. because it's more efficient and better.

so if i did this:

let mainView  = MainView()
mainView.categories = categoriesData.

then it pass it by value.

if i need to pass it by reference i could do that by using function inside the MainView()

class MainView:UIView{

    var categories:[Category]!
    fun setCategories(inout categories: Int){
            self.categories = categories;

    }

}

but if i don't want to use set function, How could i pass it by reference. e.g

mainView.categories = &categoriesData. but that doesn't work ?thanks

vacawama

Swift uses ARC (Automatic Reference Counting) when dealing with arrays, and it delays copying arrays until one of the copies is modified:

For example:

var a = [1, 2, 3, 4, 5]
let b = a
let c = a   // 1

a.append(6) // 2

print(a.count)
print(b.count)
print(c.count)

At step 1 above, there is only one copy of [1, 2, 3, 4, 5] in memory, and a, b, and c are references to it.

When a is modified in step 2, Swift gives a and new copy of the array and b and c continue to reference the original array. So now there are 2 copies of the array in memory.


Let's look at a much more involved example:

class Person: CustomStringConvertible {
    let name: String
    var friends: [Person] = []

    init(name: String) {
        self.name = name
    }

    var description: String { return name }
}

func createFredsFriends() -> [Person] {
    let barney = Person(name: "Barney")
    let wilma = Person(name: "Wilma")
    let betty = Person(name: "Betty")
    let friends = [barney, wilma, betty]  // 1

    return friends
}

func createFred() -> Person {
    let fred = Person(name: "Fred")

    let friends = createFredsFriends() // 2
    fred.friends = friends             // 3

    return fred
}

let fred = createFred()  // 4

print(fred.friends)  // [Barney, Wilma, Betty]
  • At step 1, the array of friends is created. It is referenced by the local variable friends.

  • This reference goes away when createFredsFriends() returns, and the only reference to the array is held then by the local variable friends at step 2. Ownership of the array has been passed.

  • At step 3, a second reference to the array has been assigned to the friends property of fred.

  • At step 4, createFred() has returned, so the local variable friends is gone and no longer references the array. The only reference is in the property of the fred object which is held by the variable fred.

So the array was created once, several references to it were created, and in the end there is a single reference to the array and all of this was done without a single copy operation.


Since Swift arrays are value types and copied when changed, you can't pass an array and then expect the original to be updated when the copy is. If you need that level of functionality, you can create a class wrapper for the array and then always access that array through the instance of that class.

Here I've modified the previous example to show how that would work:

// Class to wrap array so that everyone references the same copy
class FriendsWrapper {
    var friends: [Person]

    init(friends: [Person]) {
        self.friends = friends
    }
}

class Person: CustomStringConvertible {
    let name: String
    var friendsWrapper: FriendsWrapper?

    init(name: String) {
        self.name = name
    }

    func addFriend(friend: Person) {
        if let wrapper = friendsWrapper {
            wrapper.friends.append(friend)
        } else {
            friendsWrapper = FriendsWrapper(friends: [friend])
        }
    }

    var description: String { return name }
}

func createFredsFriends() -> [Person] {
    let barney = Person(name: "Barney")
    let wilma = Person(name: "Wilma")
    let betty = Person(name: "Betty")
    let friends = [barney, wilma, betty]

    return friends
}

func createFred() -> Person {
    let fred = Person(name: "Fred")

    let friendsWrapper = FriendsWrapper(friends: createFredsFriends())
    fred.friendsWrapper = friendsWrapper

    // Add friend to Fred object
    fred.addFriend(Person(name: "Bam Bam"))

    // Copy of array in local friendsWrapper is updated
    print(friendsWrapper.friends)  // [Barney, Wilma, Betty, Bam Bam]

    return fred
}

let fred = createFred()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Swift: Pass array by reference?

From Dev

C++ How to pass array by reference to function

From Dev

How come we can pass an array to a function by reference without using pointers

From Dev

How to pass reference to the function

From Dev

How to pass function by reference?

From Dev

How to pass a dynamic array out of function by reference c++

From Dev

How can I pass an array of structures to a function by reference?

From Dev

Array without function in Swift

From Dev

Array without function in Swift

From Dev

Reference to array inside function in Swift

From Dev

How to pass reference to function with lambda

From Dev

How to pass reference to function with lambda

From Dev

How to pass slice into a function by reference

From Dev

Pass array by reference between viewcontrollers in swift

From Dev

How to pass an arguments array to a function without changing context?

From Dev

How to pass a multidimensional array to a function without inner dimension in c++?

From Dev

How to pass an array of Swift strings to a C function taking a char ** parameter

From Dev

How to pass a Swift array to an Objective-C function

From Dev

how to pass a swift Array as UnsafePointer<T> argument in a function

From Dev

How to pass a function reference, instead of the function itself?

From Dev

How do you pass a CFSocket callback function a reference to self when using Swift?

From Dev

how can I pass a swift array by reference between viewControllers in prepare(for segue:?

From Dev

How to pass a function reference in a recursive directive in AngularJS?

From Dev

How to pass an object as reference or pointer to a thread function?

From Dev

How do you declare a function in which you pass a multidimensional array by reference

From Dev

How can I pass an Array value in a function that will be used reference a JSON name in Pouchdb?

From Dev

How to pass array element by reference in c++?

From Dev

How to pass a reference of a hash and array to subroutine

From Dev

How to pass Dynamic Array by reference C++

Related Related

  1. 1

    Swift: Pass array by reference?

  2. 2

    C++ How to pass array by reference to function

  3. 3

    How come we can pass an array to a function by reference without using pointers

  4. 4

    How to pass reference to the function

  5. 5

    How to pass function by reference?

  6. 6

    How to pass a dynamic array out of function by reference c++

  7. 7

    How can I pass an array of structures to a function by reference?

  8. 8

    Array without function in Swift

  9. 9

    Array without function in Swift

  10. 10

    Reference to array inside function in Swift

  11. 11

    How to pass reference to function with lambda

  12. 12

    How to pass reference to function with lambda

  13. 13

    How to pass slice into a function by reference

  14. 14

    Pass array by reference between viewcontrollers in swift

  15. 15

    How to pass an arguments array to a function without changing context?

  16. 16

    How to pass a multidimensional array to a function without inner dimension in c++?

  17. 17

    How to pass an array of Swift strings to a C function taking a char ** parameter

  18. 18

    How to pass a Swift array to an Objective-C function

  19. 19

    how to pass a swift Array as UnsafePointer<T> argument in a function

  20. 20

    How to pass a function reference, instead of the function itself?

  21. 21

    How do you pass a CFSocket callback function a reference to self when using Swift?

  22. 22

    how can I pass a swift array by reference between viewControllers in prepare(for segue:?

  23. 23

    How to pass a function reference in a recursive directive in AngularJS?

  24. 24

    How to pass an object as reference or pointer to a thread function?

  25. 25

    How do you declare a function in which you pass a multidimensional array by reference

  26. 26

    How can I pass an Array value in a function that will be used reference a JSON name in Pouchdb?

  27. 27

    How to pass array element by reference in c++?

  28. 28

    How to pass a reference of a hash and array to subroutine

  29. 29

    How to pass Dynamic Array by reference C++

HotTag

Archive