objective c - Convert javascript function to Obj-C function -


i want use bunch of functions written in javascript in obj-c app. made header file , got first 1 converted, got stuck on second. here's started , i've done far doesn't work...

function calcdayofweek(juld)     {         var = (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 attempt:

nsstring calcdayofweek(float julianday) {     float = (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 string day of week based on input of julian day number.

edit: per yuji's answer, worked...

nsstring* calculatedayofweek(float julianday) {     int = 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 learn syntax , grammar of objective-c. function be

nsstring* calcdayofweek(float julianday) {      int = ((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 objects pointers, not object itself. need nsstring* instead of nsstring.
  • @"..." objective-c string object. "..." c-string, char*.
  • i recommend against using == float. happens if 2 floats differ .00000001? well, % operator automatically gives integer, still don't it.

however, shouldn't re-invent wheel. cocoa has api calendar conversion you. see date , time programming topics.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -