# I can't understand if this is get/set or closure in Swift

**URL:** <https://forums.kodeco.com/t/i-cant-understand-if-this-is-get-set-or-closure-in-swift/15656>\
**Category:** Swift\
**Created:** [June 7, 2016, 7:47pm UTC](https://forums.kodeco.com/t/i-cant-understand-if-this-is-get-set-or-closure-in-swift/15656 "2016-06-07T19:47:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apoorvmote](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/apoorvmote/32/94441_2.png) [@apoorvmote](https://forums.kodeco.com/u/apoorvmote)\
**Post date:** [June 7, 2016, 7:47pm UTC](https://forums.kodeco.com/t/i-cant-understand-if-this-is-get-set-or-closure-in-swift/15656/1 "2016-06-07T19:47:23Z")

</div>

I am using code where its like

let date: NSDate? = {  
return nil  
}()

what does it mean when you type () at end of the code.

Is that getter?

---

<div class="post-metadata">

**Author:** ![narrativium](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/narrativium/32/110937_2.png) [@narrativium](https://forums.kodeco.com/u/narrativium)\
**Post date:** [June 7, 2016, 9:33pm UTC](https://forums.kodeco.com/t/i-cant-understand-if-this-is-get-set-or-closure-in-swift/15656/2 "2016-06-07T21:33:06Z")

</div>

That’s a closure. You’ve basically written a long form of this:  
`let date: NSDate? = nil`

The `{}` forms a closure around the instruction “`return nil`”. The `()` calls the closure, and Swift infers the closure’s arguments - it has none - from this; it infers the return type - `NSDate?` - from the declaration of the variable (`date`) you’re assigning the result of the closure to.

Getters and setters are about allowing enough access to your types (structs / classes etc.) to read useful information and allow useful configuration while hiding unnecessary detail and preventing you from accidentally making our types inconsistent. For example, `count` on an Array is a getter; you can call `array.count` and get a numerical value, but you can’t set the count of an array, and you wouldn’t want to.

Here’s one way you could write code to let someone get and set a value of type `NSDate`, so long as that date was before right now:

```auto
private var privateDate : NSDate = NSDate() // initialised to right now; hidden outside of the type.
var date : NSDate
{
  get { return privateDate }
  set {
    if newValue.compare(NSDate()) == NSOrderedAscending {
      privateDate = newValue
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![apoorvmote](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/apoorvmote/32/94441_2.png) [@apoorvmote](https://forums.kodeco.com/u/apoorvmote)\
**Post date:** [July 20, 2016, 12:00pm UTC](https://forums.kodeco.com/t/i-cant-understand-if-this-is-get-set-or-closure-in-swift/15656/3 "2016-07-20T12:00:04Z")

</div>

Thanks for the reply. It did actually help me a lot. Sorry for late reply.
