2

I want to use a bunch of functions that were written in Javascript in my Obj-C app. I made a header file and got the first one converted, but got stuck on the second. Here's what I started with and what I've done so far that doesn't work...

function calcDayOfWeek(juld)
    {
        var A = (juld + 1.5) % 7;
        var DOW =     (A==0)?"Sunday":(A==1)?"Monday":(A==2)?"Tuesday":(A==3)?"Wednesday":(A==4)?"Thursday":(A==5)?"Friday":"Saturday";
        return DOW;
    }

...and my attempt:

NSString calcDayOfWeek(float julianDay)
{
    float A = (julianDay + 1.5) % 7;
    NSString DOW = (A==0)?"Sunday":(A==1)?"Monday":(A==2)?"Tuesday":(A==3)?"Wednesday":(A==4)?"Thursday":(A==5)?"Friday":"Saturday";
    return DOW;
}

It should return a string with the day of the week based on the input of a Julian Day Number.

EDIT: Per Yuji's answer, this is what worked...

NSString* calculateDayOfWeek(float julianDay) {
    int a = fmod(julianDay + 1.5, 7);
    NSString* dayOfWeek = (a==0)?@"Sunday":(a==1)?@"Monday":(a==2)?@"Tuesday":(a==3)?@"Wednesday":(a==4)?@"Thursday":(a==5)?@"Friday":@"Saturday";
    return dayOfWeek;
}

3 Answers 3

5

You first need to learn the syntax and the grammar of Objective-C. The function would be

NSString* calcDayOfWeek(float julianDay)
{
     int A = ((int)(julianDay + 1.5)) % 7;
     NSString* DOW = (A==0)?@"Sunday":(A==1)?@"Monday":(A==2)?@"Tuesday":(A==3)?@"Wednesday":(A==4)?@"Thursday":(A==5)?@"Friday":@"Saturday";
     return DOW;
}
  • In Objective-C, variables for objects are pointers, not the object itself. You need NSString* instead of NSString.
  • @"..." is the Objective-C string which is an object. "..." is a C-string, which is just char*.
  • I recommend against using == for a float. What happens if two floats differ by .00000001? Well, % operator automatically gives you integer, but I still don't like it.

However, you shouldn't re-invent the wheel. Cocoa has an API that does the calendar conversion for you. See Date and Time Programming Topics.

Sign up to request clarification or add additional context in comments.

5 Comments

Of the suggestions, this gets the closest. But the int A = (julianDay + 1.5) % 7; line generates two instances of error: invalid operands to binary % (have 'double' and 'int')
I searched for that error and tried this instead: int A = fmod(julianDay, 7); This compiles fine - now to try it. And thanks for the link to the date/time stuff!
Ah, sorry. I had no idea that % doesn't accept float and int!
I've been curious about this for a while and it's impossible to google: What's the difference between NSString* calcDayOfWeek()... in your answer and NSString *calcDayOfWeek()... in @ConspicuousCompiler's one? Does moving the * have a syntactic impact?
No it doesn't. Read the specification of the programming language C: open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf Basically, any spaces to the right or to the left of non-alphabetic character mean nothing to the language. You can just remove them. NSString*calc... would be just fine.
1

You'll need to change the way you declare the function. Try this:

-(NSString *) dayOfWeek:(float)julianDay {

    float A = (julianDay + 1.5) % 7;
    NSString *DOW = (A==0)?@"Sunday":(A==1)?@"Monday":(A==2)?@"Tuesday":(A==3)?@"Wednesday":(A==4)?@"Thursday":(A==5)?@"Friday":@"Saturday";
    return DOW;
}

Comments

1

You'll want to build your strings as NSStrings by prefixing them with @s and take a reference to them:

NSString *calcDayOfWeek(float julianDay)
{
    float A = (julianDay + 1.5) % 7;
    NSString *DOW = (A==0)?@"Sunday":(A==1)?@"Monday":(A==2)?@"Tuesday":(A==3)?@"Wednesday":(A==4)?@"Thursday":(A==5)?@"Friday":@"Saturday";
    return DOW;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.