Kodeco Forums

macOS Development for Beginners: Part 1

In this macOS development tutorial for beginners, learn how to create your first "Hello, World" app with Swift and take a tour of Xcode.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/731-macos-development-for-beginners-part-1

My Macbook Pro canā€™t download Sierra. Do I really need Sierra? I feel itā€™s unfair.

I wrote and tested the tutorial on macOS Sierra, but it should work on earlier versions of OS X, provided you can install Xcode version 8.

1 Like

Sure there are some changes from Yosemite or some other version to Sierra, but major apps still support earlier versions and there is no problem getting started on an earlier OS. I have had a quick look at the tutorial and I think the main requirement is the ability run Xcode, so you can get started. You might find a minor difference but there is no reason to avoid it completely.

That said think about updating that MacBook some timeā€¦ itā€™s good to be current especially in the Apple world.

(Sarahā€™s answer beat me by a minute :grinning: - thereā€™s two people encouraging you.)

1 Like

Thank you for the great tutorialā€¦ Iā€™m glad there is some MacOS tutorialsā€¦

Thanks for the macOS tutorials! Iā€™ve wanted to program for the Mac for a long time. Hoping to be able to leverage my iOS knowledge.

Iā€™ve followed the tutorial but I canā€™t get the helloLabel to display the greeting. It will always only show ā€˜Helloā€™, even though the greeting variable is getting set properly to either Hello World! in the case of an empty name field, or ā€˜Hello Steveā€™ when I enter my name. Hereā€™s what I get when I print the value of the greeting variable:

Printing description of greeting:
"Hello Steve!"

Iā€™ve disconnected and reconnected the outlet for the label but no joy. Hereā€™s my code:

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var nameField: NSTextField!
    @IBOutlet weak var helloLabel: NSTextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    @IBAction func sayButtonClicked(_ sender: Any) {
        var name = nameField.stringValue
        if name.isEmpty {
            name = "World"
        }
        
        let greeting = "Hello \(name)!"
        helloLabel.stringValue = greeting
    }

}

EDIT: I figured it out. I had selected the label, then Edit > Resize to Fit Content, thinking it would automatically resize the field to fit whatever content would be in the field, now or in the future. Bad thinking. Resized label to be full screen width and all is well. It just ended up that it was truncating the text right after ā€˜Helloā€™ which threw me.

Good catch! Part 2 of this series will go into auto-layout but this part just used preset object sizes.

Very helpful tutorial but I am having one problem following it in the section mentioning ā€œGo back to Main.storyboard and click on the button to select it. In the Utilities panel on the right, make sure the Attributes Inspector is showing ā€“ the 4th button across the top.ā€

The problem I have is that Main.storyboard opens in a separate window, and when I click on the View Controller, the Attributes Inspector panel of the main XCode window just says ā€œNot Applicableā€. I am not sure how to resolve this.

I have recently upgraded to MAC OS Sierra and I got this error ā€œMac app that is damaged and canā€™t be openedā€. Anyone knows how to resolve this error?

If you have the storyboard open in a new window, you need to show the Attributes Inspector in that window to be able to select and edit UI elements.

Select your Main.storyboard window and in the menus, choose View/Utilities/Show Utilities or use the Command-Option-0 shortcut (zero, not O).

Then you can select the View Controller and edit its attributes in that separate window.

I am a bit confused as to how you could get this error with an app you have built yourself from Xcode. It is a misleading error message that usually indicates macOSā€™s security is blocking the app from running.

The sample project does not code sign the app, but I thought that apps you build yourself in Xcode were exempt from these restrictions.

Try right-clicking on the app and selecting Open to see if that can bypass the security settings, after a confirmation dialog.

One other possible fix is to go to System Preferences - Security & Privacy. In the General tab, change the ā€œAllow apps downloaded from:ā€ setting to ā€œApp Store and identified developersā€.

Hi sarah.

I need to learn Swift for development in Macbook.

so I saw your lesson,
ā€œmacOS Development for Beginners: Part 1ā€
(https://www.raywenderlich.com/151741/macos-development-beginners-part-1)

I need to buy a book included this lesson.
Which book do i have to buy? plz let me know.

from Wilshire/Vermont.

This tutorial is not included in any book, but you can buy any of the Ray Wenderlich books here: https://store.raywenderlich.com

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]

This was updated on Jun 14, 2021 by @robertomachorro, so reopening comments! :]