Consider this static factory method written in Objective-C:
@implementation User
+ (instancetype)named:(NSString *)name {
let user = [self new];
user.name = name;
return user;
}
@end
How do I implement it in Swift?
The plenty of Self/self/Type/type keywords in Swift feels fuzzy and I struggle to figure out the right solution.
The solution came out to be like this:
The following example shows everything works fine:
So, you should pay attention to these keypoints:
requiredconstructor (maybe even without arguments)Self.inside your methodclassaccess modifier-> Self(note the capital S)