Does Objective-C forbid use of structs?

前端 未结 3 775
执念已碎
执念已碎 2021-02-20 02:53

I\'m new to Objective C

I tried using a simple struct and got

arc forbids objective-c objects in struct

Looking up ARC, it

3条回答
  •  我在风中等你
    2021-02-20 03:29

    arc forbids objective-c objects in struct

    Structs are a C construct. The compiler is telling you, in very unabiguous terms, that you can't have Objective-C objects inside a struct, not that structs are illegal.

    You can use regular C structs all you want.

    Your example tries to put references to an Objective-C object, NSString, into a struct, which is incompatible with ARC.

    Structs are typically used for simple data structures. Examples that you are likely to come across in Objective-C code are CGPoint and CGRect.

    CGPoint looks something like this

    struct CGPoint 
    { 
       CGFloat x; 
       CGFloat y; 
    };
    

    A CGFloat is, I think, just a double, and the idea it to represent a point in 2D space. Structs can include pointers to other structs, C-arrays and standard C data types such as int, char, float... And Objective-C classes can contain structs, but the reverse does not work.

    Structs can also get pretty complicated, but that is a very broad topic that is best researched using Google.

提交回复
热议问题