Chapter 9 : Comparing Data

This forum contains all the assignments and questions readers may have for each chapter.

Moderator: gary.bennett

Chapter 9 : Comparing Data

Postby gary.bennett » Sat Aug 14, 2010 3:38 pm

Topics Covered:
    Introducing Boolean Logic
    Using Relational Operators
    Comparing Numbers
    Using Boolean Expressions
    Comparing Strings
    Comparing Dates
    Combining Comparisons
    Using the Switch Statement
    Grouping Variables Together
    NSArray.
    NSMutableArray
    NSDictionary
    NSMutableDictionary

Weekly Q&A for Objective-C for Absolute Beginners Book

Purpose: Answer questions about Objective-C and OOP from book or other related info.
This Webinar is held every week on Wednesday, from:
6:30 PM - 7:30 PM PST
Register Now at:
https://www2.gotomeeting.com/register/763610930
Once registered you will receive an email confirming your registration with information you need to join the Webinar.
System Requirements
PC-based attendees
Required: Windows® 7, Vista, XP or 2003 Server
Macintosh®-based attendees
Required: Mac OS® X 10.4.11 (Tiger®) or newer
Attachments
Ch09 - BookStore.zip
Chapter 9 Source Code
(44.25 KiB) Downloaded 251 times
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 9 : Comparing Data

Postby JosRuggiero » Mon Apr 30, 2012 12:49 pm

Having trouble with:

NSDate *saleDate = [NSDate dateWithString: etc...}

the error that i get is:

no known class method for selector "dateWithString"

this comes out of the copy of Objective-C that i have. A typo maybe??; but if it is I can't seem to find anything that fixes it.

any help?

thanks
joe
JosRuggiero
 
Posts: 0
Joined: Sun Mar 04, 2012 3:12 pm

Re: Chapter 9 : Comparing Data

Postby alexche » Wed Jun 06, 2012 4:09 pm

JosRuggiero wrote:Having trouble with:

NSDate *saleDate = [NSDate dateWithString: etc...}

the error that i get is:

no known class method for selector "dateWithString"

This stuff seems to be deprecated ( . Use another way. Below I posted how i did the exercise.
alexche
 
Posts: 0
Joined: Mon Jun 04, 2012 11:15 am

Re: Chapter 9 : Comparing Data

Postby alexche » Wed Jun 06, 2012 5:32 pm

Id like to share my experience in solving the main task from Chapter 9:
Write an app that determines if the following years are leap years: 1800, 1801, 1899, 1900, 2000, 2001, 2003 and 2010. Output should be written to the console in the foll. format: "2000 is a leap year" or "2001 is not a leap year"

So the the first idea on algorithm I had was to check somehow if the date Feb 29 exists for every of years in question. At the beginning I thought it would be easy - just to get it as NSDate object and to check if it's true or false. I did'nt know yet that NSDate doesn't know if the date it contains is a real one - all Feb29s were true for it))

Later I found a much easier and more known algorithm how to do it, seems it 's a common way used by almost everyone . I'm posting it here too but first - my hardest way:

HARDEST WAY

The beginning was pretty easy - creating an array. Only I had to search on how to create an array with integers-objects). It turned out the solution is NSNumber
Code: Select all
//creating an array and filling it with years to check

    NSMutableArray *yearsToCheck = [[NSMutableArray alloc] init];
    [yearsToCheck addObject:[NSNumber numberWithInt:1899]];
    [yearsToCheck addObject:[NSNumber numberWithInt:1800]];
    [yearsToCheck addObject:[NSNumber numberWithInt:1801]];
    [yearsToCheck addObject:[NSNumber numberWithInt:1900]];
    [yearsToCheck addObject:[NSNumber numberWithInt:2000]];
    [yearsToCheck addObject:[NSNumber numberWithInt:2003]];
    [yearsToCheck addObject:[NSNumber numberWithInt:2010]];


Then long figuring out how to make for every year a real date of Feb 29 and how to know if they really existed That meant to make it know about a calendar - Gregorian one) : the 5 lines below helped me to actually lay the the dates on calendar by using NSDateComponents that allows to set Year Month and Day
Code: Select all
    for (id year in yearsToCheck){  // enumeration - means for every year in the array do the following code:
       
        //these 5 lines below helped me to actually lay the the dates on calendar by using  NSDateComponents that allows to set Year Month and Day
        NSDateComponents *dateAppliedOnCalendar = [[NSDateComponents alloc] init];
        [dateAppliedOnCalendar setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
        [dateAppliedOnCalendar setYear:[year integerValue]];// setYear doesnt accept NSNumber, I had to cast integer back
        [dateAppliedOnCalendar setMonth:02];
        [dateAppliedOnCalendar setDay:29];
...
       

Here is the important moment: empirically (by means of NSlogs) I understood that when calling getter for NSDateComponents ([dateAppliedOnCalendar date]) it shows Feb 29 if it existed for leap years and !!! Mar 1 if it didnt (i.e. nonleap years). What I have left to do is to compare dates of all years to Feb 29 . Since i didnt manage to do it by comparing NSDates I decided to convert them to NSStings and compare strings . Finally that was also within the topic)
Code: Select all
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];//this stuff was needed to convert date to string
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; //this stuff allowed me convert date to the format like "Feb DD, YYYY"
        NSString *stringToCompare = [dateFormatter stringFromDate:[dateAppliedOnCalendar date]];//actually converting NSDateComponents object to NSDateObject, then to NSString
...


Finally the comparing by "Feb-29" it-self and NSLogs:
Code: Select all
       if ([stringToCompare rangeOfString:@"Feb 29"].location != NSNotFound)
        {
            NSLog(@"%@ is leap",year);
        }
        else {
            NSLog(@"%@ is not leap",year);
        }
       
       
    }



SEEMS, THE RIGHT WAY when the year is considered like a digit.
THe algorithm is the following:
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.

Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.

Here is the code ( creating the array, then if statement, that is it) :
Code: Select all
 //creating an array and filling it with years to check
    NSMutableArray *yearsToCheck = [[NSMutableArray alloc] init];
    [yearsToCheck addObject:[NSNumber numberWithInt:1899]];
    [yearsToCheck addObject:[NSNumber numberWithInt:1800]];
    [yearsToCheck addObject:[NSNumber numberWithInt:1801]];
    [yearsToCheck addObject:[NSNumber numberWithInt:1900]];
    [yearsToCheck addObject:[NSNumber numberWithInt:2000]];
    [yearsToCheck addObject:[NSNumber numberWithInt:2003]];
    [yearsToCheck addObject:[NSNumber numberWithInt:2010]];
   
    for (id year in yearsToCheck){  // for every year in the array do the following code:
      if (([year integerValue]%4 == 0 && [year integerValue]%100 !=0) ||[year integerValue]%400 ==0) {
            NSLog(@"%@ is leap",year);
        }
        else {
            NSLog(@"%@ is not leap",year);
        }
 }
alexche
 
Posts: 0
Joined: Mon Jun 04, 2012 11:15 am

Re: Chapter 9 : Comparing Data

Postby FENO75 » Fri Aug 17, 2012 11:02 pm

Hello

I am currently reading chapter 9 and Im new to objective c programming......

When creating the first project, i am not seeing the relevant files within Xcode that i should see namely: comparisonsappdelegate.h and the main.c. I am creating the project exactly like what is specified in the book. The only thing i could put it down to is that my version of x code is different from when the book was printed????

Signed

Please help
FENO75
 
Posts: 0
Joined: Tue Aug 07, 2012 3:38 am

Re: Chapter 9 : Comparing Data

Postby Elro » Wed Aug 22, 2012 5:35 am

I encountered the same frustration Feno75. If you google a little it is possible to get older versions of Xcode, but I found pushing on with the newer version although tougher will probably be best long term as it keeps you up to date with the changes in Xcode as well.

Elro
Elro
 
Posts: 0
Joined: Fri Jun 22, 2012 5:43 am

Re: Chapter 9 : Comparing Data

Postby Drago » Fri Jun 21, 2013 1:04 pm

FENO75 wrote:Hello

I am currently reading chapter 9 and Im new to objective c programming......

When creating the first project, i am not seeing the relevant files within Xcode that i should see namely: comparisonsappdelegate.h and the main.c. I am creating the project exactly like what is specified in the book. The only thing i could put it down to is that my version of x code is different from when the book was printed????

Signed

Please help


The file to edit is : AppDelegate.m,

after edit looks like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
// self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// return YES;
NSLog(@"Hello World");
}
"It's more fun to be a pirate than to join the navy."
Steve Jobs.
User avatar
Drago
 
Posts: 0
Joined: Thu Jun 20, 2013 3:41 pm


Return to Book -> Objective-C for Absolute Beginners: (2nd Edition) iPhone and Mac Programming Made Easy

cron