realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.61k stars 2.22k forks source link

Rule Request: Explicit NotificationCenter usage. #2711

Open Cyberbeni opened 5 years ago

Cyberbeni commented 5 years ago

New Issue Checklist

New rule request

  1. 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

  2. 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)

  3. Should the rule be configurable, if so what parameters should be configurable? no parameters

  4. 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.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

Cyberbeni commented 3 years ago

Based on https://github.com/realm/SwiftLint/issues/3338 it could be useful even if those old OSes are not supported.