Wishing Chico Happy Birthday with Xcode

My girlfriend and I have a schnauzer that might as well be our son.  Greeting me regularly with happiness and affection, Chico manages to make everyday a fun one. He is constantly reminding me to get exercise and take him for his walks and he is always causing mischief that keeps Alyssa and I entertained.

This past month was a special one for us because our third wheel turned 12.  Alyssa wanted to celebrate Chico’s birthday, so we got him some treats and a new toy, and sang “Happy Birthday” to him.

It was a bittersweet moment when we started thinking about how old Chico is in human years. I have always heard that in order to get a dog’s age in human years, you need to multiply their current age by 7.  Using this method, my calculations indicated Chico wasn’t far away from shuffleboard and bingo night.

Fearing how this would impact Alyssa, I went searching for a more accurate way to determine Chico’s age in human years. I read that due to the rate that a dog matures, the first 2 years are actually equivalent to 10.5 human years and that each additional dog year is equivalent to 4 human years.

Sure, one could do this equation in their head, or on a scratch sheet of paper, but that’s no fun. You should make an application that will determine your dog’s age for you using Xcode!

Before we being this tutorial, you will need to be on a Mac computer, you will need an Apple id, and of course you will need Xcode.

At this point, we’ll assume you’ve downloaded Xcode and are ready to begin. If not, installation instructions are found on the link above.

Open Xcode, and create a single view application. Give your project a name, Organization Name, Company Identifier (you can usually use self.edu.yourname) and lastly a Class Prefix.

Make sure you don’t get to funky with the class prefix. Although fun is encouraged, you don’t want to mess up your Classes, because that can create a lot of headaches. Choose something simple like TG, or your initials, but keep it short and sweet. Lastly, choose iPhone for your device.

image003-chico

You can use a variety of programming languages with Xcode, but for this tutorial we will be using Objective C. Click on your storyboard to get started.

image005-chico

Using the panel on the far right, search for 2 UILabels, a UITextbox, and a UIButton. Drag each element onto the storyboard.

image007-chicoI put one label on the left, one label on the right. In my example, I used the text on the left for Chico’s age, and deleted the text from the label on the right but kept the label. We will output Chico’s age here later.

Holding the Alt key, click on the ViewController.h file. This will allow you to view the storyboard and the ViewController.h file at the same time. Holding the Control key, drag the input area to the .h file and name it. I named my textbox ageInput. Repeat with the label that you deleted the text from earlier. Mine is called chicosAge. Lastly drag the button to the .h file and configure the options to be an action, the type as a UIButton, and for name I did ageButton.

image009-chico

Now that our structures are finally in place, it’s time to have some fun! Holding the Alt key, click on the ViewController.m file.

Find the IBAction sender function. This function fires when the button on the storyboard is pressed.

Mine looks like this: (IBAction)ageButton:(UIButton *)sender {}

image011-chico

Inside that function we will grab the value from the input box, process it with our function, and print out Chico’s age.

Since this has been dragging on. Here is the code with notes.

– (IBAction)ageButton:(UIButton *)sender {
    //This Function will run when the ageButton is clicked

    //Chicos age = the integer value of the textbox
int chicosAge = [self.ageInput.text intValue];
    //Declare variable ageInHumanYears of type int
int ageInHumanYears;
if (chicosAge < 1){
    //output this response to the chicosAge label
self.chicosAge.text = [NSString stringWithFormat:(@”You are young!”)];
}
if(chicosAge ==1){
ageInHumanYears = 10.5;
}else if(chicosAge ==2){
ageInHumanYears = 21;
}else{
        //Hes gotta be at least 21
int minimumAge = 21;
 //Subtract the 2 first years from his age and multiply by 4
ageInHumanYears = ((chicosAge-2) * 4);
        //Add minimum age to chicos age in human years
ageInHumanYears = ageInHumanYears + minimumAge;
        //output this response to the chicosAge label
self.chicosAge.text = [NSString stringWithFormat:@”Chico is %i years old”,ageInHumanYears];
}

}

Once this function is written, you’re all ready to determine your dog’s age. Press the play button in the top left corner, and if your build succeeded, you can now run your application. Type in your Dog’s age, press the button and wish your dog a Happy Birthday too!

image014-chico