Quick Cocoa App test with Swift

Discussion in 'Mac Programming' started by mfram, Jun 5, 2014.

  1. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #1
    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
            }
        }
        
    }
     

    Attached Files:

  2. cognociente macrumors newbie

    Joined:
    Aug 3, 2014
    #2
    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?
     
  3. xanrov macrumors newbie

    xanrov

    Joined:
    Feb 9, 2008
    Location:
    Asnieres, FRANCE
    #3
    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.
     
  4. cognociente macrumors newbie

    Joined:
    Aug 3, 2014
    #4
    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.
     
  5. xanrov macrumors newbie

    xanrov

    Joined:
    Feb 9, 2008
    Location:
    Asnieres, FRANCE
    #5
    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 ?
     
  6. xanrov macrumors newbie

    xanrov

    Joined:
    Feb 9, 2008
    Location:
    Asnieres, FRANCE
    #6
    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
     

Share This Page