Chapter    9

Comparing Data

In this chapter, we will discuss one of the most basic and frequent operations you will perform as you program: comparing data. In the bookstore example, you may need to compare book titles if your clients are looking for a specific book. You may also need to compare authors if your clients are interested in purchasing books by a specific author. Comparing data is a common task performed by developers. Many of the loops you learned about in the previous chapter will require you to compare data so that you know when your code should stop looping.

Comparing data in programming is like using a scale. You have one value on one side and another value on the other side. In the middle, you have an operator. The operator determines what kind of comparison is being done. Examples of operators are “greater than,” “less than,” or “equal to.”

The values on either side of the scale are usually variables. You learned about the different types of variables in Chapter 3. In general, the comparison functions for different variables will be slightly different. It is imperative that you become familiar with the functions and syntax to compare data because this will form the basis of your development.

For the purposes of this chapter, we will use an example of a bookstore application. This application will allow users to log in to the application, search for books, and purchase them. We will cover the different ways of comparing data to show how they would be used in this type of application.

Revisiting Boolean Logic

In Chapter 4, we introduced Boolean logic. Because of its prevalence in programming, we will revisit this subject in this chapter and go into more detail.

The most common comparison that you will program your application to perform is comparisons using Boolean logic. Boolean logic usually comes in the form of if/then statements. Boolean logic can have only one of two answers: yes or no. The following are some good examples of Boolean questions that you will use in your applications:

  • Is 5 larger than 3?
  • Does now have more than five letters?
  • Is 6/1/2010 later than today?

Notice that there are only two possible correct answers to these questions: yes and no. If you are asking a question that could have more than these two answers, that question will need to be worded differently for programming.

Each of these questions will be represented by an if/then statement (for example, “If 5 is greater than 3, then print a message to the user”). Each if statement is required to have some sort of relational operator. A relational operator can be something like “is greater than” or “is equal to.”

To start using these types of questions in your programs, you will first need to become familiar with the different relational operators available to you in the Swift language. We will cover them first. After that, you will learn how different variables can behave with these operators.

Using Relational Operators

Swift uses five standard comparison operators. These are the standard algebraic operators with only one real change: in the Swift language, as in most other programming languages, the “equal to” operator is made by two equals signs (==). Table 9-1 describes the operators available to you as a developer.

Table 9-1. Comparison Operators

Operator

Description

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

==

Equal to

Note  A single equals sign (=) is used to assign a value to a variable. Two equals signs (==) are needed to compare two values. For example, if(x=9) will assign the value of 9 to the variable x and return yes if 9 is successfully assigned to x, which will be in most, if not all, of the cases. if(x==9) will do a comparison to see whether x equals 9. Xcode now throws an error if you try to assign a value to a variable in an if statement.

Comparing Numbers

One of the difficulties developers have had in the past was dealing with different data types in comparisons. Earlier in this book, we discussed the different types of variables. You may remember that 1 is an integer. If you wanted to compare an integer with a float such as 1.2, this could cause some issues. Thankfully, Swift helps with this. In Swift, you can compare any two numeric data types without having to typecast. (Typecasting is still sometimes needed when dealing with other data types, which we cover later in the chapter.) This allows you to write code without worrying about the data types that need to be compared.

Note  Typecasting is the conversion of an object or variable from one type to another.

In the bookstore application, you will need to compare numbers in many ways. For example, let’s say the bookstore offers a discount for people who spend more than $30 in a single transaction. You will need to add the total amount the person is spending and then compare this to $30. If the amount spent is larger than $30, you will need to calculate the discount. See the following example:

var discountThreshold = 30
var discountPercent = 0
var totalSpent = calculateTotalSpent()

if(totalSpent > discountThreshold) {
    discountPercent = 10
}

Let’s walk through the code. First, you declare the variables (discountThreshhold, discountPercent, and totalSpent) and assign a value to them. Notice you do not need to specify the type of number for the variables. The type will be assigned when you assign it a value. You know that discountThreshold and discountPercent will not contain decimals, so the compiler will create them as Ints. In this example, you can assume you have a function called calculateTotalSpent, which will calculate the total spent in this current order. You then simply check to see whether the total spent is larger than the discount threshold; if it is, you set the discount percent. If we wanted a customer who spent exactly $30 to get the same discount, we could use a >= instead of a >. Also notice that it was not necessary to tell the code to convert the data when comparing the different numeric data types. As mentioned earlier, Swift handles all this.

Another action that requires the comparison of numbers is looping. As discussed in Chapter 4, looping is a core action in development, and many loop types require some sort of comparison to determine when to stop. Let’s take a look at a for loop:

var numberOfBooks: Int
numberOfBooks = 50

for var y = 1; y <= numberOfBooks; y++ {
    doSomething()
}

In this example, you iterate, or loop, through the total number of books in the bookstore. The for statement is where the interesting stuff starts to happen. Let’s break it down.

The following portion of the code is declaring y as a variable and then assigning it a starting value of 1:

var y = 1;

The following portion is telling the computer to check to see whether the counting variable y is less than or equal to the total number of books you have in the store. If y becomes larger than the number of books, the loop will no longer run.

y <= numberOfBooks;

The following portion of code increases y by 1 every time the loop is run.

y++

Creating an Example Xcode App

Now let’s create an Xcode application so you can start comparing numeric data.

  1. Launch Xcode. From the Finder, go to the Applications folder. Drag the folder to the Dock because you will be using it throughout the rest of this book. See Figure 9-1.

    9781484214893_Fig09-01.jpg

    Figure 9-1. Launching Xcode

  2. Click “Create a New Xcode Project” to open a new window. On the left side of that window, under iOS, select Application. Then select Single View Application on the right side. Click Next, as shown in Figure 9-2.

    9781484214893_Fig09-02.jpg

    Figure 9-2. Creating a new project

    Note  The Single View Application template is the most generic and basic of the iOS application types.

  3. On the next page, enter the name of your application. Here we used Comparison as the name, but you can choose any name you like. This is also the window where you select which device you would like to target. Leave it as iPhone for now, as shown in Figure 9-3.

    9781484214893_Fig09-03.jpg

    Figure 9-3. Selecting the project type and name

    Note   Xcode projects, by default, are saved in the Documents folder in your user home.

  4. Once the new project is created, you will see the standard Xcode window. Select the arrow next to the Comparison folder to expand it if it is not already expanded. You will see several files. The main file for your project is called AppDelegate.swift. You will also see a ViewController.swift file. This file is the source that controls the single window that is created by default for you in this type of app. For the purposes of these examples, you will be focusing on the AppDelegate.swift file.
  5. Click the AppDelegate.swift file. You will see the following code:
    func application(application: UIApplication, didFinishLaunchingWithOptions
                               launchOptions: [NSObject: AnyObject]?) -> Bool {

            // Override point for customization after application launch.
            return true
        }
  6. The method application: didFinishLaunchingWithOptions is called after each time the application is launched. At this point, your application will launch and display a window. You will add a little Hello World to your application. Before the line return true, you need to add the following code:
    NSLog("Hello World")

This line creates a new String with the contents Hello World and passes it to the NSLog function that is used for debugging.

Note  The NSLog method is available to Objective-C and Swift. It is commonly used for debugging an application because you can show information easily in the Debug area.

Let’s run the application to see how it works:

  1. Click the Run button in the default toolbar.
  2. The iOS simulator will launch. This will just display a window. Back in Xcode, a Console window will appear at the bottom of the screen, as shown in image Figure 9-4. You can always toggle this window by selecting View image Debug Area image Show/Hide Debug Area.

9781484214893_Fig09-04.jpg

Figure 9-4. Debugger window

You will now see a line of text in your debugger. The first part of the line shows the date, time, and name of the application. The Hello World part was generated by the NSLog line that you added.

  1. Go back to Xcode and open the AppDelegate.swift file.
  2. Go to the beginning of the line that begins with NSLog. This is the line that is responsible for printing the Hello World section. You are going to comment out this line by placing two forward slashes (//) in front of the line of code. Commenting out code tells Xcode to ignore it when it builds and runs the application. In other words, code that is commented out will not run.
  3. Once you comment out the line of code, you will no longer see the line in bold if you run the program because the application is no longer outputting any line.
  4. For the application to output the results of your comparisons, you will have to add one line, as shown here:
    NSLog("The result is %@", (6 > 5 ? "True" : "False"))

    Note  The previous code, (6>5 ? "True" : "False"), is called a ternary operation. It is essentially just a simplified way of writing an if/else statement.

  5. Place this line in your code. This line is telling your application to print The result is. Then it will print True if 6 is greater than 5, or it will print False if 5 is greater than 6.

Because 6 is greater than 5, it will print True.

You can change this line to test any of the examples you have put together thus far in this chapter or any of the examples you will do later.

Let’s try another example.

var i = 5
var y = 6
NSLog("The result is %@", (y > i ? "True" : "False"))

In this example, you create a variable and assign its value to 5. You then create another variable and assign the value to 6. You then change the NSLog example to compare the variables i and y instead of using actual numbers. When you run this example, you will get the result shown in Figure 9-5.

9781484214893_Fig09-05.jpg

Figure 9-5. NSLog output

Note  You may get compiler warnings when using this code. The compiler will tell you that the false portion of the ternary operator will never be executed. The compiler can look at the values while you are typing the code and know that the comparison will be true.

You will now explore other kinds of comparisons, and then you will come back to the application and test some of them.

Using Boolean Expressions

A Boolean expression is the easiest of all comparisons. Boolean expressions are used to determine whether a value is true or false. Here’s an example:

var j = 5
if  j > 0 {
    some_code()
}

The if statement will always evaluate to true because the variable j is greater than zero. Because of that, the program will run the some_code() method.

Note   In Swift, if a variable is optional and therefore not assigned a value, you should use a question mark after the variable declaration. For example, var j becomes var j:Int?.

If you change the value of j, the statement will evaluate to false because j is now 0. This can be used with Bool and number variables.

var j = 0
if j > 0 {
    some_code()
}

Placing an exclamation point in front of a Boolean expression will change it to the opposite value (a false becomes a true, and a true becomes a false). This line now asks “If not j>0,” which, in this case, is true because j is equal to 0. This is an example of using an integer to act as a Boolean variable. As discussed earlier, Swift also has variables called Bool that have only two possible values: true or false.

var j = 0
if !(j > 0) {
    some_code()
}

Note  Swift, like many other programming languages, uses true or false when assigning a value to a Boolean variable.

Let’s look at an example related to the bookstore. Say you have a frequent buyers’ club that entitles all members to a 15 percent discount on all books they purchase. This is easy to check. You simply set the variable clubMember to true if the person is a member and false if he or she is not. The following code will apply the discount only to club members:

var discountPercent = 0
var clubMember: Bool = false

if(clubMember) {
    discountPercent = 15
}

Comparing Strings

Strings are a difficult data type for most C languages. In ANSI C (or standard C), a string is just an array of characters. Objective-C took the development of the string even further and made it an object called NSString. Swift has taken the String class even further and made it easier to work with. Many more properties and methods are available to you when working with an object. Fortunately for you, String has many methods for comparing data, which makes your job much easier.

Let’s look at an example. Here, you are comparing passwords to see whether you should allow a user to log in:

var enteredPassword = "Duck"
var myPassword = "duck"

var continueLogin = false

if enteredPassword == myPassword {
    continueLogin = true
}

The first line just declares a String and sets it value to Duck. The next line declares another string and sets its value to duck. In your actual code, you will need to get the enteredPassword string from the user.

The next line is the part of the code that actually does the work. You simply ask the strings if they are equal to each other. The example code will always be false because of the capital "D" in the enteredPassword versus the lowercase "d" in the myPassword.

There are many other different comparisons you might have to perform on strings. For example, you may want to check the length of a certain string. This is easy to do.

var enteredPassword = "Duck"
var myPassword = "duck"
var continueLogin = false
if enteredPassword.characters.count > 5 {
    continueLogin = true
}

Note  count is a global function that can be used to count strings, arrays, and dictionaries.

This code checks to see whether the entered password is longer than five characters.

There will be other times when you will have to search within a string for some data. Fortunately, Swift makes this easy to do. String provides a function called rangeOfString, which allows you to search within a string for another string. The function rangeOfString takes only one argument, which is the string for which you are searching.

var searchTitle: String
var bookTitle: String
searchTitle = "Sea"
bookTitle = "2000 Leagues Under the Sea"

if bookTitle.rangeOfString(searchTitle) != nil {
     addToResults()
}

This code is similar to other examples you have examined. This example takes a search term and checks to see whether the book title has that same search term in it. If it does, it adds the book to the results. This can be adapted to allow users to search for specific terms in book titles, authors, or even descriptions.

For a complete listing of the methods supported by String, see the Apple documentation at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html.

Using the switch Statement

Up to this point, you’ve seen several examples of comparing data by simply using the if statement.

if some_value == SOME_CONSTANT {
    ...
} else if some_value == SOME_OTHER_CONSTANT {
    ...
} else if some_value == YET_SOME_OTHER_CONSTANT {
    ...
}

If you need to compare a variable to several constant values, you can use a different method that can simplify the comparison code: the switch statement.

Note  In Objective-C, you could only use integers to compare in a switch statement. Swift allows developers more freedom in using the switch statement.

The switch statement allows you to compare one or more values in an original variable.

var customerType = "Repeat"

switch  customerType {   // The switch statement followed by a begin brace
case "Repeat":           // Equivalent to if (customerType == "Repeat")
   ...                   // Call functions and put any other statements here after the case.
   ...
case "New":
    ...
    ...
case "Seasonal":                   ...
    ...
default:             // Default is required in Swift

}  // End of the switch statement.

The switch statement is powerful, and it simplifies and streamlines comparisons of a Boolean operator to several different values.

In Swift, the switch statement is a powerful statement that can be used to simplify repeated if/else statements.

Comparing Dates

Dates are a fairly complicated variable type in any language, and unfortunately, depending on the type of application you are writing, they are common. Swift does not have its own native Date type. This means developers have to use the Cocoa date type NSDate. The NSDate class has a lot of nice methods that make comparing dates easy. We will focus on the compare function. The compare function returns an NSComparisonResult, which has three possible values: OrderedSame, OrderedDescending, and OrderedAscending.

// Today's Date
var today: NSDate = NSDate()

// Sale Date = Tomorrow
let timeToAdd: NSTimeInterval = 60*60*24
var saleDate: NSDate = today.dateByAddingTimeInterval(timeToAdd)

var saleStarted = false
let result: NSComparisonResult  = today.compare(saleDate)

switch result {
case NSComparisonResult.OrderedAscending:
    // Sale Date is in the future
    saleStarted = false
case NSComparisonResult.OrderedDescending:
    // Sale Start Date is in the past so sale is on
    saleStarted = true
default:
    // Sale Start Date is now
    saleStarted = true
}

This may seem like a lot of work just to compare some dates. Let’s walk through the code and see whether you can make sense of it.

var today: NSDate = NSDate()
let timeToAdd: NSTimeInterval = 60*60*24
var saleDate: NSDate = today.dateByAddingTimeInterval(timeToAdd)

Here, you declare two different NSDate objects. The first one, named today, is initialized with the system date or your device date. Before creating the second date, you need to add some time to the first date. You do this by creating an NSTimeInterval. This is a number in seconds. To add a day, you add 60*60*24. The second date, named saleDate, is initialized with a date some time in the future. You will use this date to see whether this sale has begun. We will not go into detail about the initialization of NSDate objects.

Note  In most programming languages, dates are dealt with in a specific pattern. They usually start with the four-digit year followed by a hyphen, then a two-digit month followed by a hyphen, and then a two-digit day. If you are using a data format with a time, this data is usually presented in a similar manner. Times are usually presented with the hour, minute, and second, each separated by a colon. Swift inherits time zone support from Cocoa.

The results of using the compare function of an NSDate object is an NSComparisonResult. You have to declare an NSComparisonResult to capture the output from the compare function.

let result: NSComparisonResult = today.compare(saleDate)

This simple line runs the comparison of the two dates. It places the resulting NSComparisonResult into the variable called result.

switch result {
case NSComparisonResult.OrderedAscending:
    // Sale Date is in the future
    saleStarted = false
case NSComparisonResult.OrderedDescending:
    // Sale Start Date is in the past so sale is on
    saleStarted = true
default:
    // Sale Start Date is now
    saleStarted = true
}

Now you need to find out what value is in the variable result. To accomplish this, you perform a switch statement that compares the result to the three different options for NSComparisonResult. The first line finds out whether the sale date is greater than today’s date. This means that the sale date is in the future, and thus the sale has not started. You then set the variable saleStarted to false. The next line finds out whether the sale date is less than today. If it is, then the sale has started, and you set the saleStarted variable to true. The next line just says default. This captures all other options. You know, though, that the only other option is OrderedSame. This means the two dates are the same, and thus the sale is just beginning.

There are other methods that you can use to compare NSDate objects. Each of these methods will be more efficient at certain tasks. We have chosen the compare method because it will handle most of your basic date comparison needs.

Note  Remember that an NSDate holds both a date and a time. This can affect your comparisons with dates because it compares not only the date but also the time.

Combining Comparisons

As discussed in Chapter 4, you’ll sometimes need something more complex than a single comparison. This is where logical operators come in. Logical operators enable you to check for more than one requirement. For example, if you have a special discount for people who are members of your book club and who spend more than $30, you can write one statement to check this.

var totalSpent = 31
var discountThreshhold = 30
var discountPercent = 0
var clubMember = true

if totalSpent > discountThreshhold && clubMember {
    discountPercent = 15
}

We have combined two of the examples shown earlier. The new comparison line reads as follows: “If totalSpent is greater than discountThreshold AND clubMember is true, then set the discountPercent to 15.” For this to return true, both items need to be true. You can use || instead of && to signify “or.” You can change the previous line to this:

if totalSpent > discountThreshhold || clubMember  {
        discountPercent = 15
}

Now this reads as follows: “If totalSpent is greater than discountThreshold OR clubMember is true, then set the discount percent.” This will return true if either of the options is true.

You can continue to use the logical operations to string as many comparisons together as you need. In some cases, you may need to group comparisons using parentheses. This can be more complicated and is beyond the scope of this book.

Summary

You’ve reached the end of the chapter! Here is a summary of the topics that were covered:

  • Comparisons: Comparing data is an integral part of any application.
  • Relational operators: You learned about the five standard relational operators and how each is used.
  • Numbers: Numbers are the easiest pieces of information to compare. You learned how to compare numbers in your programs.
  • Examples: You created a sample application where you could test your comparisons and make sure that you are correct in your logic. Then, you learned how to change the application to add different types of comparisons.
  • Boolean: You learned how to check Boolean values.
  • Strings: You learned how strings behave differently from other pieces of information you have tested.
  • Dates: You learned how difficult it can be to compare dates and that you must be careful to make sure you are getting the response you desire.

Exercises

  • Modify the example application to compare some string information. This can be in the form of a variable or a constant.
  • Write a Swift application that determines whether the following years are leap years: 1800, 1801, 1899, 1900, 2000, 2001, 2003, and 2010. Output should be written to the console in the following format: The year 2000 is a leap year or The year 2001 is not a leap year. See http://en.wikipedia.org/wiki/Leap_year for information on determining whether a year is a leap year.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset