ebook img

Objective-C for Absolute Beginners: iPhone, iPad, and Mac Programming Made Easy PDF

340 Pages·2011·2.75 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Objective-C for Absolute Beginners: iPhone, iPad, and Mac Programming Made Easy

CHAPTER 3: It’s All About the Data 51 Figure 3–11. Adding a bunny to our world We need to declare a variable of type String. The variable will hold the string, “The sum of 2 + 3 is:5”. 13. Click “Edit Code” in the bottom right of the scene to go back to our Editor. 14. Select the Bunny instance. From the Procedures Tab, drag the this.bunny say text:??? procedure tile to the editor. See Figure 3–12. 52 CHAPTER 3: It’s All About the Data Figure 3–12. Adding the procedure (method) say to the editor 15. Click on Custom TextString and enter the string “The sum of 2 + 3 is:” as the parameter value. See Figure 3–13. Figure 3–13. Entering the string parameter CHAPTER 3: It’s All About the Data 53 16. Click OK and then click on the first parameter for the say procedure. Append the totalSum to our first parameter String. See Figure 3–14. Figure 3–14. Adding the totalNumber to our customized string to display to the user Alice did something very nice for us in the last step. It automatically converted the data type totalSum from an integer to a string when it appended its value to the “The sum of 2 + 3 is:”. We will learn how to do this using Objective-C. You can run the program now and will notice the customized string doesn’t display for very long. To increase the display time of our customized string, click on the option for a second parameter of the say procedure and change the duration to 2 seconds or any other value you like. See Figure 3–15. 54 CHAPTER 3: It’s All About the Data Figure 3–15. The editor section 17. Press the play button and if you’ve done everything correctly, your app should look like Figure 3–16 when it runs. Figure 3–16. The app has run successfully! Data Types and Objective-C Now that we have covered the principles of data types and have written an Alice app to help show how these principles apply, let’s write an Objective-C app that accomplishes what we just did in Alice. In Objective-C, we have similar data types as we did in Alice. Some of the most frequently used data types for storing numbers are integers, doubles, floats, and longs. CHAPTER 3: It’s All About the Data 55 Table 3–5 lists many of the basic data types. Many of these will be covered in later chapters. Table 3–5. Objective-C basic data types Type Examples Specifiers char ‘a’, ‘0’,’\n’ %c int 42, -42, 550 0xCCE0, 099 %i, %d, unsigned int 20u, 101U, 0xFEu %u, %x, %o long int 13, -2010, 0xfefeL %ld, unsigned long int 12UL, 100ul, 0xffeeUL %lu, %lx, %lo long long int 0xe5e5e5LL, 501ll %lld unsigned long long int 11ull, 0xffeeULL %llu, %llx, %llo float 12.30f, 3.2e-5f, 0x2.2p09 %f, %e, %g, %a double 3.1415, %f, %e, %g, %a long double 3.1e-5l %Lf, %Le, %Lg, %La id Nil %@ Our Objective-C app will add two integers and display their sum to the console. The app will also display the text “The program has successfully terminated.” This will be fun and easy, so let’s get started. 1. As iOS developers, Xcode is where we make our living, so open up Xcode and create a new project. To do this, select File ➤ New project and select the options shown in Figure 3–17. Click on Next. 56 CHAPTER 3: It’s All About the Data Figure 3–17. Opening a new project NOTE: One of the most common issues new students have when creating a command line app is finding the project in their version of Xcode. Figure 3–18 shows Xcode Version 4.2 on the Lion (10.7) operating system. Your version of Xcode may be newer or older and menus and selection options may be different. So, look around in the File ➤ New project settings for the equivalent options. If you have difficulty finding these options, visit our forum for this book at forum.xcelme.com and go to this chapter. We will be happy to answer your questions. 2. Save the Product Name as Chapter 3 (see Figure 3–18). Then select the directory to save your project and click on Next. CHAPTER 3: It’s All About the Data 57 Figure 3–18. Project settings. 58 CHAPTER 3: It’s All About the Data Figure 3–19. When created, and selecting the main.m file, your Xcode project should look this 3. After you create the project, you need to open your source code file in your editor. Open the main.m source file. (see Figure 3–19) If you haven’t seen “//” used in computer programming before, it enables the programmer to comment about his or her code. Comments are not compiled by our applications and are used as notes for the programmer or, more importantly, for programmers who follow the original developer. Comments help both the original developer and follow-up developers understand how the app was developed. Sometimes, it is necessary for comments to span several lines or just part of a line. This can be accomplished with the /* and the */. All the text between the /* and the */ are treated as comments and are not compiled. In our example, we first need to declare and initialize our variables firstNumber and secondNumber. It is good practice to always initialize variables when they are declared or soon afterwards. We'll then increment the variables firstNumber and secondNumber by 1. We’ll print the sum of firstNumber and secondNumber. Finally, we will print to the console, “The program has terminated successfully.” See Figure 3–20 CHAPTER 3: It’s All About the Data 59 Figure 3–20. Code for printing to the console NSLog is a function that can take one or more parameters. The first parameter is generally the string that is to be printed to the console. The @ symbol in front of the string tells the compiler this is an Objective-C type string and not a C++ string. The @ symbol is typically used in front of all your strings for iPhone apps. If you don’t use the @ symbol, you will probably get a compiler error. NSLog is a very helpful function used by developers to test the execution of their code. %d tells the compiler an integer will be printed and to substitute the value of the integer for the %d. See Table 3–5 for other NSLog formatting specifiers. Finally, our second parameter is the integer to be printed. Figure 3–21 shows the completed executed output of our application. To compile and run your application, click on the “Run” button on your toolbar. We can see that we printed out the NSLog string along with the notice at the end by the debugger saying the app's execution completed successfully.

Description:
You have a great idea for an app, but where do you begin? Objective-C is the universal language of iPhone, iPad, and Mac apps, and Objective-C for Absolute Beginners, Second Edition startsyou on the path to mastering this language and its latest release. Using a hands-on approach, you'll learn how t
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.