Rule Request: Explicit NotificationCenter usage.
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
New rule request
-
Why should this rule be added? Share links to existing discussion about what
the community thinks about this.
We are making a Mac application and some of the older macOS versions still store targets without incrementing their weak reference count, so forgetting to remove the observer will result in a crash, that's why we made utility functions for NSObjects to store the observations (since they can also be block based, just removing self wouldn't be enough) and remove them from notification center when the object deallocates.
According to this, iOS9 and macOS 10.11 are the minimum version where this is not needed anymore:
https://developer.apple.com/documentation/foundation/notificationcenter/1413994-removeobserver -
Provide several examples of what would and wouldn't trigger violations.
Trigger:
NotificationCenter.default.addObserver(self, selector:selector, name:.notificationName, object:nil
NotificationCenter.default.addObserver(forName:.notificationName, object: nil, queue: nil, using: {})
Not trigger:
NotificationCenter.default.post(.notificationName) -
Should the rule be configurable, if so what parameters should be configurable?
no parameters -
Should the rule be opt-in or enabled by default? Why?
opt-in since not everyone has to support OS versions where this is a problem.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation file or test is named. Start by reviewing existing SwiftLint rule implementations and rule tests to find the appropriate entry points, then verify that selector- and block-based addObserver calls are flagged, post calls are not, and the rule is opt-in.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100