【发布时间】:2012-09-17 07:33:48
【问题描述】:
我在 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
}
但我无法使用构造函数进行编译。我希望在创建结构时这些值采用一些默认值。
我该怎么做?
【问题讨论】:
-
我很谨慎,我需要在启动某些操作时正确初始化值并且我不希望出现故障
-
这个结构体有什么原因不能转换成简单的类吗?
-
__unsafe_unretained不适用于BOOLs -- 它们是基元,而不是对象。
标签: objective-c struct constructor initialization