Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Objective-C forbid use of structs?

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 looks like this is the specification that defines Objective C syntaxt - is that correct?

Secondly, how do I go about using struct if it's not allowed?

Thank you!

Edit: Some code as a sample

@implementation Cities {
    // The goal is to have a struct that holds information about a city,
    // like when a person started and ended living there.
    // I was trying to make this struct an instance variable of the Cities
    // class
    // XCode doesn't like the below struct definition

    struct City
    {
        NSString *name;
        int *_startYear;
        int *_endYear;
    };
}
like image 745
user2490003 Avatar asked Feb 12 '15 07:02

user2490003


People also ask

When should I use structs in C?

You can use it to store variables in different types. The struct type is comparable to classes in object-oriented programming. Sometimes you may need to assign values to objects with the same properties. Instead of creating multiple variables for these objects in your C program, you can define them in a struct.

Is Objective-C slower than C?

Objective-C is slightly slower than straight C function calls because of the lookups involved in its dynamic nature.

Why does Objective-C use yes and no?

It's just syntax, there's no technical reason for it. They just use YES/NO for their BOOL instead of true/false like c++ does.

How do you create a structure in Swift?

Unlike other programming languages, Swift doesn't require you to create separate interface and implementation files for custom structures and classes. In Swift, you define a structure or class in a single file, and the external interface to that class or structure is automatically made available for other code to use.


1 Answers

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.

like image 198
dandan78 Avatar answered Sep 22 '22 23:09

dandan78