将ANSI C移植到Objective C所需的最小更改是什么?

Mon*_*ess 0 c objective-c ipad

作为Mac新手,我需要将大约5,000行ANSI C转换为Objective C才能在iPad应用中使用.大多数代码与下面的示例类似.为了节省时间并最大限度地减少错误,我只想在绝对需要移植到Obj C时更改原始C代码.为了帮助我理解转换,必须更改下面代码的哪些部分才能连接到iPhone/iPad用户界面?真的很感激任何指导.谢谢您的帮助.

void GetLandingSpeeds(LandingSpeeds *mySpeeds, int PhenomType, LandingInterpolationParameters *InterParams, char *FlapLand, char *WingStab)
{

    mySpeeds->LandingSpeedsOK = No;

    sprintf(query_string,"SELECT * FROM Landing_Speeds WHERE Weight = %d AND FlapLand = '%s' AND WingStab = '%s'", InterParams->Weight_lower, FlapLand, WingStab);
    Query* GetFlapsSpeeds_Lower = sql_select_query(query_string, AircraftDatabase);

    if (InterParams->Weight_Interpolation_Percent == 0)
    {
        // single table

        if (GetFlapsSpeeds_Lower->RecordCount == 1) 
        {
            mySpeeds->Vac = atoi(sql_item(GetFlapsSpeeds_Lower, "Vac"));
            mySpeeds->Vref = atoi(sql_item(GetFlapsSpeeds_Lower, "Vref"));
            if (PhenomType == P300)
                mySpeeds->Vfs = atoi(sql_item(GetFlapsSpeeds_Lower, "Vfs"));
            mySpeeds->LandingSpeedsOK = Yes;
        }
    }
    else
    {
        // simple linear interpolation        
        sprintf(query_string,"SELECT * FROM Landing_Speeds WHERE Weight = %d AND FlapLand = '%s' AND WingStab = '%s'", InterParams->Weight_upper, FlapLand, WingStab);
        Query* GetFlapsSpeeds_Upper = sql_select_query(query_string, AircraftDatabase);

        if ( (GetFlapsSpeeds_Lower->RecordCount == 1) && (GetFlapsSpeeds_Upper->RecordCount == 1) )
        {
            mySpeeds->Vac = atoi(sql_item(GetFlapsSpeeds_Lower, "Vac")) * (1-InterParams->Weight_Interpolation_Percent) + atoi(sql_item(GetFlapsSpeeds_Upper, "Vac")) * InterParams->Weight_Interpolation_Percent;
            mySpeeds->Vref = atoi(sql_item(GetFlapsSpeeds_Lower, "Vref")) * (1-InterParams->Weight_Interpolation_Percent) + atoi(sql_item(GetFlapsSpeeds_Upper, "Vref")) * InterParams->Weight_Interpolation_Percent;
            if (PhenomType == P300)
                mySpeeds->Vfs = atoi(sql_item(GetFlapsSpeeds_Lower, "Vfs")) * (1 - InterParams->Weight_Interpolation_Percent) + atoi(sql_item(GetFlapsSpeeds_Upper, "Vfs")) * InterParams->Weight_Interpolation_Percent;
            mySpeeds->LandingSpeedsOK = Yes;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*ier 10

Objective-C是C的纯超集.你不应该改变任何东西.你看到了什么问题?

编辑

搜索"模型 - 视图 - 控制器"或"MVC".这是iOS编程的核心.Model类可以跨平台高度重用,并且可以毫无困难地在C中使用.您在上面发布的是经典的模型代码.你要求它提供数据; 它为您提供数据.

View类是高度特定于平台的,您可以从iOS本身获得大部分类.这些是按钮,图形和图像等.他们只知道如何在屏幕上绘制数据.

Controller类将两者粘合在一起,是您需要在Objective-C中从头开始编写的.他们要求Model(C)代码中的数据,并更新Views(ObjC).

如果您的5000行C主要是模型代码(听起来像是来自您的描述),它应该很容易插入.您只需要编写Objective-C来管理UI.

  • 我有一个正方形,但有人告诉我,我需要一个矩形.我需要更换广场吗? (3认同)
  • 没有要求将C端口移植到Objective-C.Objective-C恰好是C的超集.你在C中可以做的一切都将在Objective-C中完全相同,没有任何变化.我无法想象为什么你需要重写SQL.这既不是C也不是Objective-C.如果您正在更改SQL后端,则只需要这样做.您不应该只将函数转换为方法.方法是面向对象编程的一部分.如果您转换为方法,则需要重新构建,而不是端口.但除非你想要OOP,否则没有理由这样做. (2认同)