Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an Objective-C struct in constructor?

I´m using a struct on Objective-C to store some data, something like this:

@interface Interface : NSObject
{
    // my Data
    struct Data
    {
        __unsafe_unretained BOOL isInit;
        __unsafe_unretained BOOL isRegister;
        __unsafe_unretained NSString* myValue;
        
       // Data() : isInit(false), isRegister(false), myValue(@"mYv4lue") {} // Constructor doesnt work
    };

    struct Data myData;  // Create Struct
}

But I can't compile with a constructor. I want the values take some default value when I create the struct.

How can I do this?

like image 708
vgonisanz Avatar asked Sep 17 '12 07:09

vgonisanz


People also ask

How do you initialize a struct in a constructor?

For instance, with this struct: struct Point { int x; int y; int z; }; We can initialize an instance with the following syntax: Point p = {1, 2, 3};

How do you initialize a struct in Objective-C?

Initializing Structures The fastest way to initialize a structure is to enclose the values in a pair of braces: Date today = {5, 22, 2011}; You can list fewer values, and they will be filled in until the end of the values you list. The remaining values will be undefined.

How do you initialize a structure object?

When initializing an object of struct or union type, the initializer must be a non-empty, (until C23) brace-enclosed, comma-separated list of initializers for the members: = { expression , ... }

Can we have constructor for struct in C?

3. Constructor creation in structure: Structures in C cannot have a constructor inside a structure but Structures in C++ can have Constructor creation.

What is constructors and creating objects in Objective C?

What is Constructors and Creating Objects in Objective C ! Constructor/initializer is a method which we call while creating a new object in Object Oriented Programming. In this method we write all the code related to initial setup of an object. Objective C have a default construction called init which is present in its root class NSObject.

How to initialize an object in a struct?

If it's global, you first need to create the object and then initialize it: class my_class { my_struct * s1; my_class () : s1 (new my_struct ()) { s1->i = 10; s1->name = "anyname"; } }; Show activity on this post.

What is a struct constructor?

In the same way, a constructor is a special method, which is automatically called when an object is declared for the class, in an object-oriented programming language. So, combining these two different methodologies, we can say that when these constructors are defined inside a Struct, then these would be referred to as Struct constructors.

What are the steps of object initialization in Objective-C?

Objective-C Lesson 11: Object Initialization 1 Allocation. Creating a new object in Objective-C is usually a two-step process. ... 2 Convenience Initializer. Most classes also have convenience initializers, which take arguments with which to set the ivars. ... 3 Constructors. ...


1 Answers

You could initialize a struct as follows using static object for default settings.

typedef struct
{
    BOOL isInit;
    BOOL isRegister;
    __unsafe_unretained NSString* myValue;

} Data;
static Data dataInit = { .isInit = NO, .isRegister = NO, .myValue = @"mYv4lue"};

Data myCopyOfDataInitialized = dataInit;
like image 160
maninvan Avatar answered Oct 06 '22 00:10

maninvan