Here is the Swift code:
class Dependency1 {
}
class Object1 {
private let dependency: Dependency1
init(dependency: Dependency1 = Dependency1()) {
self.dependency = dependency
}
}
And the Objective C equivalent: @interface Dependency1 : NSObject
@end
@implementation Dependency1
@end
@interface Object1 : NSObject
- (instancetype)initWithDependency:(Dependency1 *)dependency;
- (instancetype)init;
@end
@interface Object1()
@property (nonatomic, strong, readonly) Dependency1 *dependency;
@end
@implementation Object1
- (instancetype)initWithDependency:(Dependency1 *)dependency {
self = [super init];
if (self) {
_dependency = dependecy;
}
return self;
}
- (instancetype)init
{
return [self initWithDependency:[[Dependency1 alloc] init]];
}
@end
Thats 9 lines of code in Swift and 31 lines of code in Objective C. If I have to type 3 times more code every time I want to break up big class into a few smaller ones, I am going to be much less likely to do it.In other words, the sheer verbosity of Objective C will make even the most disciplined developer start cutting corners to reduce effort, which ultimately leads to codebases which are messier, harder to test and less flexible.
And finally, looking at the example above, is it really true that Objective C is easier to learn and understand than Swift? The code forces you to learn:
* What is an NSObject?
* How to create a private category on an class
* What does "alloc init" do, and why is it different than "new"?
* What is the difference between `self.dependency` and `_dependency`, and why does one let me change a "readonly" property?
* Why would `super init` return nil, and what does that mean?
For sure Swift also has its share of nuance, but I feel like the Objective C nuance is just baggage, while the Swift nuance opens the door to a richer feature set.
[edited] updated code for correctness