将变量传递给类/实例的最佳方法?

时间:2013-07-11 07:34:07

标签: ios class variables

我有一个实例,它根据一年中的某一天返回地球与太阳的距离。因为它在内部计算今天的日期,所以它是独立的。但是我想改变它,所以我把它作为一个整数传递给它。这样做的最佳方式是什么?我有什么选择吗?

-(double) calculateDistranceFromSun
{
    // Calculate Day of the Year
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
    setDayOfYear = dayOfYear;
     // Source: curious.astro.cornell.edu/question.php?number=582
    double Rad2Deg       = Pi/180;
    double Theta         = (dayOfYear/(DaysInYear/360)*Rad2Deg);
    double CC            = cos(Theta);
    setDistanceFromSun   = SemiMajorAxis*(1-pow(Eccentricy,2))/(1+Eccentricy*CC);
    return setDistanceFromSun;
}

1 个答案:

答案 0 :(得分:1)

-(double) calculateDistranceFromSunWithDate:(NSDate *)date
{
    // Calculate Day of the Year
    NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:date];
    setDayOfYear = dayOfYear;
     // Source: curious.astro.cornell.edu/question.php?number=582
    double Rad2Deg       = Pi/180;
    double Theta         = (dayOfYear/(DaysInYear/360)*Rad2Deg);
    double CC            = cos(Theta);
    setDistanceFromSun   = SemiMajorAxis*(1-pow(Eccentricy,2))/(1+Eccentricy*CC);
    return setDistanceFromSun;
}