Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 3. Xcode > Working with Project Files

3.3. Working with Project Files

In Chapter 2, you discovered how to make a simple calculator that could add integers but not numbers with decimals. To change the way the application operates, you'll need to alter the code in the project:

  1. In your main project window of Xcode, click to expand the Classes group.

    The Classes group opens and displays the class files in your project. What you probably recognize as a folder, Xcode calls a group. The small folder icons that appear to the left side of the project window are groups. Clicking the disclosure triangle located on the left side of each group displays the files in that group.

  2. Click the Fusebox.m file.

    The code for the Fusebox.m file displays, as shown in Figure 3-4.

    Figure 3.4. The Classes group organizes the source code files in your project.

  3. Change the code in the Fusebox.m file to read like this:

    #import "Fusebox.h"
    
    @implementation Fusebox
    
    - (IBAction)calculateAnswer:(id)sender
    {
        float num1,num2,answer;
    
        num1 = [numberField1 floatValue];
        num2 = [numberField2 floatValue];
        answer = num1 + num2;
    
        [answerField setFloatValue:answer];
    }
    
    @end

    This code has three important changes:

    • You're changing the three variables from an int type to a float type. The float data type supports numbers with decimals.

      float num1,num2,answer;

    • Because you're working with float data types now, use the floatValue function to retrieve decimal data from the two number fields.

      num1 = [numberField1 floatValue];
      num2 = [numberField2 floatValue];

    • Use the setFloatvalue function instead of the setIntValue function to display the float result in answerField.

      [answerField setFloatValue:answer];

  4. Now that you've edited the source code in Fusebox.m, select the Fusebox.h file to view its contents.

    Fusebox.h holds the definitions for your interface.

  5. In the Groups & Files list, open the Other Sources group folder and select the main.m file.

    In this file, you see code that looks like this:

    #import <Cocoa/Cocoa.h>
    
    int main(int argc, const char *argv[])
    {
        return NSApplicationMain(argc, argv);
    }

    This code appears in every Cocoa application project that you create. It's responsible for making your application go, much like a set of keys makes an automobile run. The nice part is that Xcode automatically adds it to the project for you, and you usually don't need to make any changes to this file.

  6. Choose BuildBuild and Go or press +R to see your code changes in action.

  7. After Xcode compiles and launches the project, test the Simple Calculator application, with decimal numbers.

    The calculator now adds decimals properly (see Figure 3-5).


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial