Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks.
Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related You must be explicit and consistent. This table shows how the structure of the code can be broken down into smaller pieces:. There are three parts to each line of code in this example: an object, a message, and an input value.
Not all Objective-C code is exactly like this. Some actions have no input, for example. Most of this tutorial focuses on practical examples instead of theory, but there are a few ideas that need an explanation before we dive in.
A variable is simply a label for a container. It holds a value, and the value can change at any time. A variable can hold any type of data, such as text, images or numbers. For example, a variable called milesPerGallon might have an initial value of That same variable could be changed to another value. An example of that is shown here:. In Objective-C, variables must be declared before they are used. Here are some simple examples:. Each of these lines declares a new variable, prefixed by a type.
The type states what kind of data the variable can hold. The variable milesPerGallon is declared as an int , which is an integer number.
The id type can hold any object. If you don't know what an object is, don't get stuck on it now. We'll learn more about that soon. There are many variable types defined by Cocoa itself, and you can create your own. For this tutorial, we're only going to use the generic id type. An assignment is a line of code that assigns a value to a variable, just like in algebra.
For example, here we assign a line of text to a variable called order. The symbol in front of the text is an Objective-C convention, which we're not ready to get into the details of just yet. Methods are a list of instructions for the computer to perform. The list is given a name, so that the same list of instructions can be performed over and over. For example, the list of instructions from the first section could be grouped as one method called driveToWork , shown below:.
Calling the driveToWork method is essentially the equivalent of calling all of those other lines of code individually. It's not just to save space. If you need to change the contents of a method later, you can do it in just one place.
Methods can have both input and output, though neither is required. Output from one method can be used as input for another. Here are some simplistic examples:. We use assignments to store the method output in variables.
For example, the currentSpeed method returns a value which we assign to the speed variable. When writing code, things that may seem trivial can actually be very important. Even leaving out a single character or putting particular parts of code in the wrong order can cause errors. When Xcode sees incorrect code, it can typically only display an error because it doesn't know what your intentions are. It's up to you, as the programmer, to fix the error. It's important to understand some basic formatting rules.
The spacing between words in code might seem strange. In English, an instruction to exit the freeway looks like this:. It's easier for the computer to understand the code if certain words are grouped together.
Not all methods are spaced exactly like this. You'll learn more about this as you go. In English, a period. In Objective-C, each instruction ends with a semicolon. If you leave it out, you'll get error messages. Some instructions span multiple lines. In those cases, you put a semicolon at the end of the final line only:.
You can put these multiple-line instructions on one line if you like. Formatting them across multiple lines is just meant to make the code easier to read. Simplified The examples in this section are designed to explain concepts more than specific rules.
They use a less formal structure than what you'd find in most Cocoa code. To write an application which manages photos, you might start by creating a Photo class. Each photo has variables which define unique attributes about the item, such as the ones listed below:.
These variables are called instance variables , or simply attributes. The NeXT property list format is a file format that can describe, using plain text, all of the objects used in Cocoa programs. Basically, dictionaries provide a way to name a bunch of objects, and refer to them by name.
Note that while dictionary keys must be strings, the values can be any type of object. Every property list has a root dictionary object, which can contain sub-objects. A simple plist file might look like:. The DefaultKeyBindings. It is a normal property list, which as keys, uses the requested key bindings, and as values uses the commands they should execute. If I put the following in my DefaultKeyBinding.
The way it works is simple. For lower-case and capital letters, simply type the letter into the string. For numbers and symbols, including space, the same holds.
To add modifier keys, add a symbol corresponding to that modifier:. Also, there is a difference between numbers above the letter keys, and numbers on the keypad. To indicate keypad keys, use :. This is done by Apple to ensure that applications can use these shortcuts for themselves, and not worry about user key bindings. We escape it! The app brings up a floating palette which prints a log of all the keys typed.
Of course, sometimes we wish we had more keys on the keyboard. Even with all of the modifier keys, it gets hard to keep different functionality separated, and things get confused. For this reason, it is possible to use multiple-keystroke bindings. To this end, instead of binding our first keystroke to a selector, we bind it to a dictionary, containing the second keystrokes. Emacs, the UNIX-based text editor, has many such multiple-keystroke bindings.
Therefore, as an example of how this can be done, we will take Emacs bindings, and try to replicate them in Cocoa Text Widgets. We can effectively have 4 or 5 keyboards for our bindings! But of course, the number of bindings is not our only limitation so far. We are also limited by the range of selectors offered by the text box. We often find ourselves performing complicated actions repeatedly, and would like to invoke them with single keystroke.
Mostly when this happens, Applescript, or some other programming language is the best solution. On occasion, however, the text system has enough power and flexibility to do the trick. This is because, besides single selectors, a binding can also execute a list of selectors, sequentially. This somewhat contrived action can be broken into steps: 1. Select paragraph 2. Make all the letters uppercase 3. Move to the end of the paragraph 4. Insert two new lines. Any additional feedback? Note The files that are used in this example are created in the topic How to write to a text file.
Submit and view feedback for This product This page. View all page feedback. In this article.
0コメント