1

I know a difference between computed property and closure with this link, Difference between closure and Computed property.

We are able to write closure in two ways, see an example,

var computedProperty:String{
    return "computedProperty"
}

var clouserFirst = { (name: String) -> String in
    return "Hello \(name)"
}

var clouserSecond:String = {
    return "Hello clouserSecond"
}()

If I write both closure in my class then,

the second closure is called the first time which explains in above link and after that only return those value.
But the first type of closure is calling when we called this closure and closure are a reference type. see closure in apple document.

I know that computed property is work same like function as not store any value in memory.

So, my question is how memory allocation happens in both ways of closure. Can anyone help me to understand this functionality and difference between this two closures?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
vikas prajapati
  • 1,868
  • 2
  • 18
  • 26

2 Answers2

2

In the first closure a parameter is passed as argument, and your var is of (String) -> String type while in the second is a closure initialization where you initialize a String var with a closure

The main difference is the type of both variables one is of String type while the other is (String) -> String type as closure, in other words in the first case you are declaring a String and assigning a value while in the second you are declaring a closure as var and assigning a value too

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • @Reninier Melian When class memory allocate then which clouser memory allocated and which size? – vikas prajapati Feb 02 '18 at 06:31
  • I know that when class allocated then all properties memory are allocated but not in functions because functions are the only user properties and return value. Same as computed property are not allocate any memory but only calculation and returning value. then in which clouser is processing and which clouser store value? I think you got my point. – vikas prajapati Feb 02 '18 at 06:42
  • @vikasprajapati check this answer maybe can help you https://stackoverflow.com/questions/39149006/how-is-memory-allocated-for-closures-what-is-its-storage-mechanism – Reinier Melian Feb 02 '18 at 06:46
1

You have defined two stored properties here.

  1. First one is of type closure which takes a String argument and returns a String
  2. Second one is of type String(not a closure) which gets its value from a closure(you have called it at the initialization time itself)

These two properties will be allocated the memory at the initialization of the object they are in.

Lets try to debug and see if we are correct.

enter image description here

In the above screenshot, I have stopped the debug pointer just after creating the Test object. You can see in the debug console that both the stored properties have been initialized and their respective types.

In the second screenshot I have printed the memory address of these properties/variables, to confirm that they have indeed been created at the initialisation time. closureFirst does not even need to be called to get the memory allocation and the closure which created the stringFromClosure has been destroyed after it has returned the string which is being held by stringFromClosure.

enter image description here

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33