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;
}
You first need to learn the syntax and the grammar of Objective-C.
The function would be
NSString*instead ofNSString.@"..."is the Objective-C string which is an object."..."is a C-string, which is justchar*.==for afloat. 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.