# Why this pattern?

**URL:** <https://forums.swift.org/t/why-this-pattern/66392>\
**Category:** Using Swift\
**Tags:** off-topic\
**Created:** [July 26, 2023, 10:47pm UTC](https://forums.swift.org/t/why-this-pattern/66392 "2023-07-26T22:47:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [July 26, 2023, 10:47pm UTC](https://forums.swift.org/t/why-this-pattern/66392/1 "2023-07-26T22:47:41Z")

</div>

This is as much about programming in general as it is about swift.

From UIKit, we get this:

```swift
func touchesBegan(
    _ touches: Set<UITouch>,
    with event: UIEvent?
)

```

This function would be almost pointless without an event object. The docs don't suggest you would ever not get an event. So why is this an Optional?

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [July 26, 2023, 10:56pm UTC](https://forums.swift.org/t/why-this-pattern/66392/2 "2023-07-26T22:56:42Z")

</div>

It’s optional in Swift because it’s marked `nullable` in Objective-C. That annotation is presumably there for a reason, but that reason is out of scope for the Swift forums.

---

<div class="post-metadata">

**Author:** ![aggie33](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aggie33/32/29896_2.png) [@aggie33](https://forums.swift.org/u/aggie33)\
**Post date:** [July 26, 2023, 11:00pm UTC](https://forums.swift.org/t/why-this-pattern/66392/3 "2023-07-26T23:00:10Z")

</div>

But also, I think the function is still useful even without an event, because you still have the set of touches; you can get their position as well as other useful info about them.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [July 26, 2023, 11:07pm UTC](https://forums.swift.org/t/why-this-pattern/66392/4 "2023-07-26T23:07:10Z")

</div>

Yep, that's why I said almost. Maybe useless is a bit strong. This probably devolves to complaints about Apples, docs, which I'll skip.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [July 26, 2023, 11:08pm UTC](https://forums.swift.org/t/why-this-pattern/66392/5 "2023-07-26T23:08:20Z")

</div>

Yeah, I was hoping there was some more general pattern being used which I didn't know about.

---

<div class="post-metadata">

**Author:** ![tgoyne](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@tgoyne](https://forums.swift.org/u/tgoyne)\
**Post date:** [July 26, 2023, 11:57pm UTC](https://forums.swift.org/t/why-this-pattern/66392/6 "2023-07-26T23:57:25Z")

</div>

I don't think UIKit ever calls touchesBegan() with a nil event, but it's an API where you override a method and then manually forward the event on if you didn't handle it, and that forwarding may not properly forward the event. It unfortunately originally permitted forwarding `nil`, and it's difficult to change it to non-nullable after the fact.

If the same thing were being designed today in Swift I suspect the event parameter would not be optional.

---

<div class="post-metadata">

**Author:** ![halleygen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/halleygen/32/1165_2.png) [@halleygen](https://forums.swift.org/u/halleygen)\
**Post date:** [July 27, 2023, 7:34am UTC](https://forums.swift.org/t/why-this-pattern/66392/7 "2023-07-27T07:34:57Z")

</div>

In some older UIKit APIs, collections are made nullable and nil is used instead of an empty collection. One of the most prominent examples is UICollectionView’s indexPathsForSelectedItems property:

> The value of this property is an array of NSIndexPath objects, each of which corresponds to a single selected item. **If there are no selected items, the value of this property is nil**.

I’m not sure if this is the case for touchesBegan, though.
