简体   繁体   English

将javascript函数转换为Obj-C函数

[英]Convert javascript function to Obj-C function

I want to use a bunch of functions that were written in Javascript in my Obj-C app. 我想在Obj-C应用程序中使用一堆用Javascript编写的函数。 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... 编辑:根据Yuji的答案,这是有效的...

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 . 您首先需要学习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. 在Objective-C中,对象的变量是指针,而不是对象本身。 You need NSString* instead of NSString . 您需要NSString*而不是NSString
  • @"..." is the Objective-C string which is an object. @"..."是作为对象的Objective-C字符串。 "..." is a C-string, which is just char* . "..."是一个C字符串,它只是char*
  • I recommend against using == for a float . 我建议不要将==用作float What happens if two floats differ by .00000001? 如果两个浮点数相差.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. 可可有一个API为您执行日历转换。 See Date and Time Programming Topics. 请参阅日期和时间编程主题。

You'll want to build your strings as NSStrings by prefixing them with @ s and take a reference to them: 您将需要使用@ s 作为前缀将字符串构建为NSString ,并对其进行引用:

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;
}

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM