Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 18, 2018, 09:25:17 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Chapter 15, Ex 1 - Warnings I do not understand  (Read 1945 times)
daviddelmonte
Full Member
***
Posts: 123



WWW Email
« on: January 12, 2010, 10:07:19 AM »

Please note, I have the flu. I built this mainly by copying code chunks. There are three warnings. I can't think how to resolve them.

INTERFACE

Code: (Objective-C)
//
//  elapsedDays.h
//  15.1ElapsedDays
//
//  Created by David DelMonte on 1/12/10.
//  Copyright 2010 Bohemian Technologies. All rights reserved.
//


#import <Foundation/Foundation.h>

@interface NSCalendarDate (ElapsedDays)
-(unsigned long) numberOfElapsedDays: (NSCalendarDate *) theDate;
@end


IMPLEMENTATION

Code: (Objective-C)

//
//  elapsedDays.m
//  15.1ElapsedDays
//
//  Created by David DelMonte on 1/12/10.
//  Copyright 2010 Bohemian Technologies. All rights reserved.
//

#import "ElapsedDays.h"

#define IS_LEAP_YEAR(y) (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)

@implementation NSCalendarDate (ElapsedDays)

-(unsigned long) numberOfElapsedDays: (NSCalendarDate *) theDate
{
    int             years, months, days;
    unsigned long   totDays = 0;
    NSCalendarDate *minDate, *maxDate;
    int             maxDateYear, maxDateMonth, minDateYear, minDateMonth;
    int             dayPerMonth[12] = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    if (self < theDate) {
        minDate = self;
        maxDate = theDate;
    } else {
        minDate = theDate;
        maxDate = self;
    }
    
    [maxDate years:     &years
months: &months     //warnings here
days: &days         // here, and
hours: NULL         // here
minutes: NULL
   seconds: NULL
sinceDate: minDate
];
    
    minDateYear  = [minDate yearOfCommonEra];
    maxDateYear  = [maxDate yearOfCommonEra];
    minDateMonth = [minDate monthOfYear];
    maxDateMonth = [maxDate monthOfYear];    
    
    int y = minDateYear;
    int m = minDateMonth;
    
    // loop as long as the maxDate is not reached
    while (!(y == maxDateYear && m == maxDateMonth)) {
        if (m != 2)
            totDays += dayPerMonth[m - 1];
        else {
            if IS_LEAP_YEAR(y)
                totDays += 29;
            else
                totDays += 28;
        }

        // calculate the next date in term of year and month
        if (m == 12) {
            y++;
            m = 1;
        } else
            m++;  
    }
    
    // add the residue in day
    totDays += days;
    
    return totDays;

}

@end




CODE

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "ElapsedDays.h"

int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSCalendarDate *basisDate = [[NSCalendarDate alloc] initWithYear: 2000
  month: 1
day: 1
hour: 0
 minute: 0
 second: 0
timeZone: nil
];
    NSCalendarDate *mobileDate = [[NSCalendarDate alloc] initWithYear: 2010
month: 1
 day: 12
hour: 11
  minute: 45
  second: 0
timeZone: nil
 ];

    unsigned long days = [basisDate numberOfElapsedDays: mobileDate];
    
    NSLog(@"\nBetween %@ and %@ there are %lu days.", basisDate, mobileDate, days);
NSLog(@"\nTime flies when you're having fun");

    [pool drain];
    return 0;
}

The warning message for each is: " Passing Argument x of 'years, months, days...... :sinceDate: from incompatible pointer type"
« Last Edit: January 12, 2010, 10:10:18 AM by daviddelmonte » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: January 12, 2010, 10:42:31 AM »

First of all, I forgot to mention in this assignment that the NSCalendarDate class has been deprecated (meaning you should no longer use it as the class will disappear in a future OS release), and the NSDate class should be used instead.


With that said, the arguments that are giving the errors are supposed to be of type NSinteger *, which as you recall, ends up being either an int or long int based upon whether you're compiling a 32- or 64-bit application.   This can lead to problems since you are supplying just an int * data type, thus the reason for the warning messages.

Cheers,

Steve
Logged
daviddelmonte
Full Member
***
Posts: 123



WWW Email
« Reply #2 on: January 12, 2010, 12:13:35 PM »

Well, I've tied myself up in knots trying to move from NSCalendarDate to NSDate. I will wait to complete this till I'm clear-headed..

David
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.