Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Mac OS X Development Tutorial for Beginners Part 3: Your First OS X App

In this Mac OS X Development tutorial for beginners, learn how to create your first OS X app: a magic 8-ball app!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1386-mac-os-x-development-tutorial-for-beginners-part-3-your-first-os-x-app

Hi Sam

Great tutorial!

I am in need of a code for a button in Xcode that will allow me to run a .jar file? I knoe how to do this in apple script and have compiled my own app outside of xcode but now how to do it in xcode appears to be a massive case of trial and error. Could you please assist or direct me in anyway?

Please assist

Hiya @jacobenvosloo

I don’t have any specific experience of what you’re asking, but I would suggest that you take a look at NSTask:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTask_Class/

This allows you to run an external program as a subprocess within your Cocoa app. You can also monitor the process and interact with it.

sam

Thank you

I managed with this tutorial https://www.raywenderlich.com/125071/nstask-tutorial-os-x

Regards

1 Like

Hi Sam, (I think of Beckett instead of Davis)

I went through the tutorial and keep getting an error in the viewDidLoad section. I’ve even gone as far as copying your ViewController code and pasting it in and still get the error.
The error is: Thread 1: EXC_BAD_INSTRUCTION (code-EXC_I386_INVOP, subside = 0x0)

Could this be caused by something when I was stacking, pinning, and auto layout adjust?

I’m new to the Swift / Mac programming and am at a loss.

Great tutorial too, thank you.

Hi @duff

The problem with EXC_BAD_INSTRUCTION is that they’re quite difficult to diagnose. My guess in your situation is an implicitly unwrapped optional (i.e, !) which isn’t unwrapping correctly. The most likely place you’ll come across that in this tutorial is with an unconnected @IBOutlet.

Notice that all the @IBOutlet properties are all defined as implicitly unwrapped optionals - that tells the complier that they’ll definitely exist at runtime, so don’t worry about it. If (as I’m guessing in your case) they don’t exist at runtime then you’ll see a crash like the one you’re seeing.

This is because they’re not connected to the storyboard elements correctly. You need to open the storyboard and view controller side-by-side and check that the view elements are correctly connected to the outlets in code.

Hope that’s helpful. It’s a common problem, and I think that error message should be able to give you more info. Something along the lines of “unexpectedly found nil when unwrapping optional”.

sam

Thanks Sam! Your explanation was much clearer than any of the others I had come across. Seems I was misinterpreting one of the instructions in the tutorial. That lead to the unwrapping / nill situation. So I asked the magic 8 if Sam was a good guy and the result was “Totally”. :wink:

1 Like

:8ball: knows where it’s at…

1 Like

Hello Sam,

Great tutorial for beginners. It is clearly and nicely written.

I just want to make a comment on something, I just started with OS X applications development, I am using El Capitan with XCode 8.2.1. XCode is complaining about the “hidden” property of NSView (NSTextField), it has “isHidden” (https://developer.apple.com/reference/appkit/nsview/1483369-ishidden), once corrected the error disappear. Worth updating the tutorial?.

As @duff for some reason my tests also reveal, when asked about Sam’s goodness, “Totally” as result. :wink:

Thank you very much again for such a great tutorial.

Hi @fjrivash

Thanks for pointing out the change in naming. That is part of the so-called “grand-renaming” that accompanied Swift 3. Lots of boolean properties are now prefixed with is.

Regarding updating the tutorial, the macOS team has it in its sights to bring this fully up-to-date with macOS Sierra and Swift 3. Hopefully it’ll be done some time in the near future.

Glad you enjoyed the tutorial—thank you for your kind words.

sam

There is no “update frames” available in the “resolve auto layout issues” button from the bottom of Xcode. From the “editor” menu there is, but it’s always grayed out.

I cannot get the “Piece Of Advice” text to line up inside the magic 8 ball image (it looks like it’s all lined up in the IB).

Hi @abadird

Make sure that you’ve provided the required constraints for both the label and the image. It sounds like there are missing constraints—that’ll result in the behaviour you’re seeing where it looks right in IB but not at runtime.

The “Update Frames” menu won’t appear if you don’t have the right thing selected, and if there aren’t any updates available. Ensure you’ve selected the right label, and that the constraints are present.

sam

Hello @samdavies

Using Swift 3, Xcode 8.2 and have a problem with “Add the following line to the handleWelcome(_:slight_smile: method body:”
welcomeLabel.stringValue = "Hello \(nameTextField.stringValue)!"


Can you help me? thanks

Hi @artnosenko,

Your problem is that you need to add that line between the braces of the handleWelcome(_:) method. It should look like this:

@IBAction func handleWelcome(_ sender: Any) {
  welcomeLabel.stringValue = "Hello \(nameTextField.stringValue)!"
}

You’ve currently got the line just in the root of the class, which isn’t legal. It needs to be within a method.

Hope that helps

sam

1 Like

Hello @samdavies
I have a second question. Hope you can help me.

Once again, click the main view and use the Resolve Auto Layout Issues\All Views in View Controller\Update Frames to update the layout

But, I can not find and update the menu “Update Frames”.


I use Xcode 8.2, swift 3

The “Update Frames” has been removed. You now have to click the first icon (currently grayed out) on the left. It looks like a “Refresh web page” icon. You need to know that if your frames are set up correctly then the icon will be greyed out so you can not click it. Just drag some views around and it will change to normal and then click it. Hope this helps.

1 Like

Awesome beginners tutorial :).
I really liked the simple and short.
I have updated it to Swift 3. Basically, its just a two step update from the download link. Here is the link if it helps someone : https://github.com/Darkwonder/RW-Mac-OS-X-Development-Tutorial-for-Beginners-Part-3-Your-First-OS-X-App

1 Like

@darkwonder thanks so much for doing that and posting it here!

Yw @samdavies. I am learning slowly about macOS dev. If you need an update for a tutorial just drop me line/link.

1 Like

@samdavies My son and I are very impressed with your tutorial; he was challenged to build an app for his school’s STEM Fair, and we really enjoyed working together on this. Everything went well until we reached the Initial Setup near the end (the ball and the words were in place when it ran, and we were able to click back and forth between the 8 and the advice). When we got the the Initial Setup step, we followed your directions and, later, even downloaded your completed example to compare our coding (everything looks alike to my untrained eye, although we did leave out the input name part). However, for whatever reason, when we run the app now we are unable to click to the advice side. It is driving me crazy because I know I’m doing something wrong. From the main storyboard, the click gesture recognizer is enabled, and nothing changed aside from coding between when it was clickable and now. Any thoughts?

1 Like