swiftlang/swift

[SR-6417] Check if super is called when required.

Open

#48,967 创建于 2017年11月17日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)Swift (69,989 star) (10,719 fork)batch import
compilerfeaturegood first issueimprovementswift evolution proposal neededtype checker

描述

Previous ID SR-6417
Radar rdar://problem/17408107
Original Reporter WWessels (JIRA User)
Type Improvement
Votes 5
Component/s Compiler
Labels Improvement, LanguageFeatureRequest, StarterProposal
Assignee mb (JIRA)
Priority Medium

md5: 8ca04a71f5feba734a5bda3679466acc

Issue Description:

When creating a subclass of NSObject, you get an error when you 'forget' to call super in the init() function.

When creating a subclass of UIViewController, and you forget to call super in viewDidLoad, viewWillAppear, etc, you will not get an error or warning.

When creating a subclass of UIView, and you forget to call super in your implementation of awakeFromNib, you will not get an error or warning.

When creating a subclass of UITableViewCell or UICollectionViewCell, and you forget to call super in your implementation of prepareForReuse, you will not get an error or warning.

And there are probably more places where it is required, but not guarded that you call super.

As a freelancer, I produce and see lots of code and this is one of the issues that I often see (and fix). Forgetting to call super in these functions results in unexpected behaviour, even crashes and it can be pretty hard to find the problem.

It would be very helpful if one could declare that an overriding function has to call super. For instance with an attribute in the same manner as @discardableResult:

@callToSuperRequired
func viewDidLoad() {

}

This attribute should be valid for all subclasses that override this function, even subclasses of subclasses.

And then Apple should use this in their declaration of the aforementioned functions.

贡献者指南