Use C Struct in Objective C

后端 未结 2 546
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 01:56

In an Xcode project I have a C file with functions, it compiles and works OK

I want to wrap my C code in struct(s), how will I be able to call them in Objective-C?

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 02:20

    Objective-C is a proper superset of C. Anything you can do in C can be done identically in Objective-C. So, you really don't need to think of them as different languages; Objective-C is simply "C plus some more stuff".

    // this struct is compatible with C and Obj-C
    struct fruit {
        int a;
    };
    
    int main()
    {
        struct fruit apple;
        apple.a = 1;
    
        return 0;
    }
    

    Then, any C or Objective-C source file can access that struct. There aren't any additional complications introduced by Objective-C.

提交回复
热议问题