如何在构造函数中初始化Objective C结构?

时间:2022-12-10 21:25:48

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

我在Objective C上使用一个结构来存储一些数据,如下所示:


@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.

但是我无法使用构造函数进行编译。我希望在创建Struct时值会采用一些默认值。

How can I do this?

我怎样才能做到这一点?

4 个解决方案

#1


18  

Structs don't have initializers, if you want to create a struct with a particular set of values you could write a function that returns creates and initialises it for you:

结构没有初始化器,如果要创建具有特定值集的结构,可以编写一个返回创建并为其初始化的函数:

For example

struct Data {
        BOOL isInit;
        BOOL isRegister;
        NSString* myValue;
};

Data MakeInitialData () {
    data Data;
    data.isInit = NO;
    data.isRegister = NO;
    data.myValue = @"mYv4lue";

    return data;
}

now you can get a correctly set up struct with:

现在你可以得到一个正确设置的结构:

Data newData = MakeInitialData();

A note, though; you seem to be using ARC, which doesn't work well with structs that have object pointers in them. The recommendation in this case is to just use a class instead of a struct.

但是要注意;你似乎在使用ARC,它不适用于有对象指针的结构。在这种情况下的建议是只使用类而不是结构。

#2


4  

You can also do this way:

你也可以这样做:

@interface Interface : NSObject
{

   typedef struct tagData

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

        tagData(){
           isInit = NO;
           isRegister = NO;
           myValue = NULL;
        }
    } myData;

}

#3


3  

The space where you're doing this -- between curly braces at the beginning of the class's @interface block -- doesn't allow running code. It is solely for declarations of ivars. You really shouldn't even be declaring structs in there (I'm surprised that compiles).

您正在执行此操作的空间 - 在类的@interface块开头的花括号之间 - 不允许运行代码。它仅用于宣告伊萨克斯。你真的不应该在那里宣布结构(我很惊讶编译)。

Move the constructor call to your class's init method. That's where initialization of ivars is supposed to happen in ObjC.

将构造函数调用移动到类的init方法。这就是在ObjC中发生ivars初始化的地方。

#4


3  

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;

#1


18  

Structs don't have initializers, if you want to create a struct with a particular set of values you could write a function that returns creates and initialises it for you:

结构没有初始化器,如果要创建具有特定值集的结构,可以编写一个返回创建并为其初始化的函数:

For example

struct Data {
        BOOL isInit;
        BOOL isRegister;
        NSString* myValue;
};

Data MakeInitialData () {
    data Data;
    data.isInit = NO;
    data.isRegister = NO;
    data.myValue = @"mYv4lue";

    return data;
}

now you can get a correctly set up struct with:

现在你可以得到一个正确设置的结构:

Data newData = MakeInitialData();

A note, though; you seem to be using ARC, which doesn't work well with structs that have object pointers in them. The recommendation in this case is to just use a class instead of a struct.

但是要注意;你似乎在使用ARC,它不适用于有对象指针的结构。在这种情况下的建议是只使用类而不是结构。

#2


4  

You can also do this way:

你也可以这样做:

@interface Interface : NSObject
{

   typedef struct tagData

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

        tagData(){
           isInit = NO;
           isRegister = NO;
           myValue = NULL;
        }
    } myData;

}

#3


3  

The space where you're doing this -- between curly braces at the beginning of the class's @interface block -- doesn't allow running code. It is solely for declarations of ivars. You really shouldn't even be declaring structs in there (I'm surprised that compiles).

您正在执行此操作的空间 - 在类的@interface块开头的花括号之间 - 不允许运行代码。它仅用于宣告伊萨克斯。你真的不应该在那里宣布结构(我很惊讶编译)。

Move the constructor call to your class's init method. That's where initialization of ivars is supposed to happen in ObjC.

将构造函数调用移动到类的init方法。这就是在ObjC中发生ivars初始化的地方。

#4


3  

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;