I created a simple Cocoa app to display the screen resolution. I verified that Swift makes it pretty easy to use the same method I used in Obj-C to create the same app. I will show you how similar they are.
The apps use text field outlets to display the values and button actions to trigger the fields getting set. I created a controller object as subclass of NSObject and instantiated that controller in the xib. The screenshot shows the app output.
I think the code looks pretty similar between Obj-C and Swift, so I think the new language and I will get along just fine. Well, so far. The bad news is that Xcode 6 crashed about 4 times while connecting Outlets and Actions in Swift. So all is not quite right with Xcode so far.
Sure enough, the Swift version is a little less verbose. That's nice!
Obj-C:
And the Swift version:
The apps use text field outlets to display the values and button actions to trigger the fields getting set. I created a controller object as subclass of NSObject and instantiated that controller in the xib. The screenshot shows the app output.
I think the code looks pretty similar between Obj-C and Swift, so I think the new language and I will get along just fine. Well, so far. The bad news is that Xcode 6 crashed about 4 times while connecting Outlets and Actions in Swift. So all is not quite right with Xcode so far.
Sure enough, the Swift version is a little less verbose. That's nice!
Obj-C:
Code:
@interface ScreenResController : NSObject
- (IBAction)aButton:(NSButton *)sender;
@property (weak) IBOutlet NSTextField *frameXLabel;
@property (weak) IBOutlet NSTextField *frameYLabel;
@property (weak) IBOutlet NSTextField *visibleFrameXLabel;
@property (weak) IBOutlet NSTextField *visibleFrameYLabel;
@property (weak) IBOutlet NSButton *sButton;
@property (weak) IBOutlet NSButton *dButton;
@end
@implementation ScreenResController
- (void) awakeFromNib {
[self aButton:self.sButton];
}
- (IBAction)aButton:(NSButton *)sender {
NSScreen *ms = [NSScreen mainScreen];
NSRect r = ms.frame;
NSRect vf = ms.visibleFrame;
if (sender == self.sButton)
{
self.frameXLabel.stringValue = [NSString stringWithFormat:@"%.1f", r.size.width];
self.frameYLabel.stringValue = [NSString stringWithFormat:@"%.1f", r.size.height];
self.visibleFrameXLabel.stringValue = [NSString stringWithFormat:@"%.1f", vf.size.width];
self.visibleFrameYLabel.stringValue = [NSString stringWithFormat:@"%.1f", vf.size.height];
}
else
{
self.frameXLabel.doubleValue = r.size.width;
self.frameYLabel.doubleValue = r.size.height;
self.visibleFrameXLabel.doubleValue = vf.size.width;
self.visibleFrameYLabel.doubleValue = vf.size.height;
}
}
@end
And the Swift version:
Code:
import Cocoa
class SwiftController : NSObject
{
@IBOutlet var frameX : NSTextField
@IBOutlet var frameY : NSTextField
@IBOutlet var visibleFrameX : NSTextField
@IBOutlet var visibleFrameY : NSTextField
@IBOutlet var sButton : NSButton
@IBOutlet var dButton : NSButton
override func awakeFromNib()
{
self.aButton(sButton)
}
@IBAction func aButton(sender : NSButton)
{
let ms = NSScreen.mainScreen()
let f = ms.frame
let vf = ms.visibleFrame
if (sender === sButton)
{
self.frameX.stringValue = "\(f.size.width)"
self.frameY.stringValue = "\(f.size.height)"
self.visibleFrameX.stringValue = "\(vf.size.width)"
self.visibleFrameY.stringValue = "\(vf.size.height)"
}
else
{
self.frameX.doubleValue = f.size.width
self.frameY.doubleValue = f.size.height
self.visibleFrameX.doubleValue = vf.size.width
self.visibleFrameY.doubleValue = vf.size.height
}
}
}