3 ms·
> The Swift version of our Objective-C application, with the same user interface and features, is only 39% as large This makes a lot of sense. In Objective-C c
by morbidhawk 11y ago
> The Swift version of our Objective-C application, with the same user interface and features, is only 39% as large
This makes a lot of sense. In Objective-C chaining too many method calls together gets ugly and you end up having to break it up into multiple lines of code for your own sanity. Take the code below, autocompletion is guaranteed to break on you for the Objective-C version.
Objective-C:
[self.view convertPoint:apoint toView:[[UIApplication sharedApplication] keyWindow]];
Swift:
view.convertPoint(apoint, toView: UIApplication.sharedApplication().keyWindow)
Objc Refatored:
UIApplication *application = [UIApplication sharedApplication];
UIWindow *window = [application keyWindow];
[self.view convertPoint:apoint toView:window];
- aaronbrethorst 11y ago[self.view convertPoint:apoint toView:[UIApplication sharedApplication].keyWindow]; No reason to skip the other places where dot notation would be useful.
- morbidhawk 11y agoOk my example was a bad example because keyWindow is a property. But I find myself breaking up lines of code over and over again just for the reason that it can't figure out how to autocomplete due to the bracket hell. And I leave it broken apart because if I don't now a later refactoring likely will anyway.
- Charybdis 11y agoChances are, the window you want is the window the view is in (which is probably the same as your app's window), UIView has a handy property for that. [self.view convertPoint:apoint toView:self.view.window];