Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mfram

Contributor
Original poster
Jan 23, 2010
1,370
427
San Diego, CA USA
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:
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
        }
    }
    
}
 

Attachments

  • Screen Shot 2014-06-05 at 00.28.07.png
    Screen Shot 2014-06-05 at 00.28.07.png
    26.9 KB · Views: 427
NSTextField!

In XCode6 and building a very similar example to yours given above and the compiler is forcing me to declare the text field as:
NSTextField! and not simply as NSTextField...

Any idea why that would be (I know it's making it an implicitly unwrapped optional) given that yours works without it?
 
NSTextField

What beta version of Xcode 6 are you, guys,using ?
I tried successively beta 3 where I could write for an NSTextfield :

testfield.setStringValue(string) and it worked OK as anyone would expect.

Whereas with beta 4 I'm told that "setStringValue is not a member of NSTexrField" which seems to imply that Objective C was changed or than that version is buggy.
 
I am using beta 4

Methods that begin with "set" do not seem to be available via the intellisense...

setStringValue() method has been replaced (relative to Objective-C) with a stringValue property from what I can see.
 
NSTextField

I'm having a problem with this setValue() method:
let 'res' be the name of a Swift string and textField an instance of NSTextField :
If I write : textField.setValue(res) , the complier does'nt complain, builds OK but my app hangs when I trigger the method.
My app is an elementary applet with just 2 textFields and a button. I verified that the hanging comes from this very line.
How the deuce is the right Swift way of setting a String in an NSTextField ?
 
NSTextField

Found out how to set a string in an NSTextField :

Just write : textField.stringValue = "string"

Must be the same for other types (Int, Double etc) or for other instances of View classes
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.