0

What happen when use a class type variable as a function parameter? Is it create any object? I am not clear this point

func action(gestureRecognizer: UIGestureRecognizer) {
   var touchPoint = gestureRecognizer.locationInView(self.map)
}

Is gestureRecognizer an object? I know object is declare by following way

let gestureRecognizer = UIGestureRecognizer()

Please , give suggestion to clear my concept. Thanks

Update

  override func viewDidLoad() {
    super.viewDidLoad()

   let uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")

    uilpgr.minimumPressDuration = 2

    map.addGestureRecognizer(uilpgr)


}

 func action(gestureRecognizer: UIGestureRecognizer) {

    print("Gesture Recognized")

}

Alamin
  • 877
  • 2
  • 12
  • 17
  • 1
    It is not created, it is passed by caller. `let a = A()` is created, then passing to `action(a)`. – kientux Oct 29 '15 at 17:27
  • I am not clear. Can you explain it more details. Who is the caller? – Alamin Oct 29 '15 at 17:40
  • 1
    Whatever call `action(a)` is the caller. Before the function get called, the parameter is created, not in the function. – kientux Oct 30 '15 at 02:12

1 Answers1

0

Class types passed to functions by reference, it meens that you have to create your class object, then pass it like param to function and inside function you will work with pointer to your object that was created.

For example:

class Foo {
    var a: Int = 0
}

func test(f: Foo) {
    f.a = 10
}

var _f = Foo() // here creates class object and `a` value is `0`

test(_f)  // After this call we pass the reference to our object to function

print(_f.a) // here `a` value is `10`
Alexey Pichukov
  • 3,377
  • 2
  • 19
  • 22
  • I am clear your answer. I have updated my question. Please check it. I have create a `action` function that can take a parameter. But when call this function , no object is created and didn't assign that as parameter. So , How the `action` function is working here? – Alamin Oct 30 '15 at 13:32
  • You create the `class` object `uilpgr`, at this moment the `ARC` mechanism has increased its reference count by 1 (retain), then you pass the pointer to your object to other object `map`, where it saves and its reference count is increased by 1 and became 2. When you came out of the `viewDidLoad` method, the reference count decreased by 1(release) and became 1(its not 0 and object not deleted). Then happens some action and a pointer to the object send to `action` method (+1), after end of method (-1) Object is still stored somewhere, and reference count > 0, so it is not removed – Alexey Pichukov Oct 30 '15 at 14:11