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

时间:2011-08-11 02:56:16

标签: objective-c 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;
        }
    }
}

1 个答案:

答案 0 :(得分:10)

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

修改

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

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

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

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