Uncategorized

Learn Cocoa on the Mac

Looking at the code that Xcode has generated, you see that these are both marked with IBOutlet. This is short for Interface Builder Outlet and is how you tell the storyboard editor that these object names are available for linking to a visual object. For the button, the code does not need to have a name for it, but it does need to know when a user clicks the button.

Select the button and Control-Drag into ViewController. This time, change the Connection popup to Action and set the name to sayButtonClicked. This creates the function that will be called when the button is clicked.

Hello World!

Everything is now in place to edit the code. If you had hidden the Navigator , click the toggle button in the top right, or press Command-1 to jump directly to the Project Navigator. The complete code in ViewController. The blobs beside the line numbers indicate a connection to the interface in the storyboard. Now type in your name and click the button again to see your own personal greeting. Sometimes, we programmers make mistakes — hard to believe I know, but trust me, it happens.

Cocoa Programming

And when it does, we need to be able to debug our code. Xcode allows us to stop the code at any point and step through line by line, checking the values of the variables at each point so that we can find the error. Go to sayButtonClicked in ViewController.


  • Learn Cocoa on the Mac, Second Edition [Book]?
  • Beyond Band 1: Ready ... fight! (German Edition).
  • From the archives—One PC user ponders switching sides as Windows seems to lose its "wow.".
  • Bis ins Koma (German Edition)!
  • Whats Eating....YOU?!

A blue pointed rectangle will appear. This is an active breakpoint and when you click the button, the debugger will stop here. Click it again and it will turn pale blue.

Navigation menu

It is now an inactive breakpoint and will not stop the code and start the debugger. To remove the breakpoint completely, drag it out of the line numbers gutter. Add the breakpoint again and run the app. Click the Say Hello button.

macOS Development for Beginners: Part 1 | www.newyorkethnicfood.com

Xcode will come to the front with the breakpoint line of code highlighted. In the bottom of the Editor panel, there will now be two new sections: The Variables section shows the variables used in this function as well as self — the View Controller, and sender — the button. Above the Variables display is a set of buttons for controlling the debugger.

Getting Started

Mouse over each one and read the tooltop to see what it does. Click the Step Over button to move to the next line. In the Variables display, you can check that name is an empty string, so click Step Over twice more. Select the name variable in the Variables display and click the Quick Look button below to see the contents.


  • Learn Cocoa – Mac Software Development for Newbies.
  • Briefe an Anna (German Edition);
  • Learn Cocoa on the Mac by Jack Nutting.

Now click the Print Description button to see the information printed in the Console. When you have checked the contents of the name variable, click the Continue program execution button to stop debugging and let the program move on. Use the button in the top right to hide the Debug area. In addition to code and user interfaces, your app will also need some artwork. Due to the different screen types Retina and non-Retina , you often need to provide multiple versions of each asset.

To simplify this process, Xcode uses Asset Libraries to store and organize the assets that accompany the app. In the Project Navigator , click on Assets. The only item there so far is AppIcon which will contain the various images needed to display the app icon in all the required resolutions. Click on AppIcon — you can see that it wants 10 different images to cover all the possibilities, but if you supply any one of these, Xcode will use it as best it can.

This is not good practice, as you should supply all the required icon sizes, but for this tutorial one icon will be sufficient. Download the sample icon which is a x pixel image. Drag it into the Mac pt 1x box. Build and run the app to see the icon in the Dock menu. If you still see the default app icon, quit the HelloWorld app, go back to Xcode and choose Clean from the Product menu, then run the app again. As well as being an editor, Xcode also contains all the documentation you will need for writing macOS apps.

Make sure Swift is the selected language, then click the top search result so that you can read all the details about buttons and button properties. There is also a way to get to relevant documentation directly from your code. Go back to ViewController. Option-click on the word stringValue. A popup appears with a short description. At the bottom of the popup is a link to Property Reference.

Click this link and the documentation will open to show more information. Option-clicking is often a really good way to learn, and you can even add documentation to your own functions so that it shows up in the same way.

Learn Cocoa on the Mac, Second Edition

The Help menu also includes Xcode Help for specific information about the Xcode environment. In the next part of this tutorial series, you will start to create a more complicated app. Hope to see you there! We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion.

If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center , please edit the question. Andy Matuschak has a great blog post that leads you through several good Cocoa tutorials, explaining why you are reading each one. Cocoa Dev Central has loads of tutorials. Most Cocoa programmers I know learned from this book including myself.

The third edition was released recently, so it's fairly up to date. Be sure to check out http: It goes from the very fundamentals of programming to learning Cocoa, Xcode and more. Big Nerd Ranch The definitive class to take Buy a book, open XCode, and write. Seriously, writing is the best way to learn Cocoa.

I have been working on learning Cocoa myself recently and have found Apple's own Cocoa resources to be incredibly helpful. For example projects I have spent quite a bit of time in the Adium source. Adium is a relatively large project so I am very often able to find examples of whichever concept I am interested in. The CocoaDev wiki can also be quite useful. Best books I encountered are those from Apress, in this case: Beginning iPhone 4 Development.

Was much clearer to me than those from O'Reilly. Good ways to Learn Cocoa?