Ios Swift 3创建NSNumber对象

Ios Swift 3创建NSNumber对象,ios,objective-c,swift,nsnumber,Ios,Objective C,Swift,Nsnumber,我正在尝试创建一个NSNumber对象。 我在objc中有以下代码: @property (nonatomic, assign) Enum someEnum; static NSString *const value_type = @"type"; - (id)initWithDictionary:(NSDictionary *)dict andUID:(int)uid { self.someEnum = [[dict objectForKey:value_type] integ

我正在尝试创建一个NSNumber对象。 我在objc中有以下代码:

 @property (nonatomic, assign) Enum someEnum;

 static NSString *const value_type = @"type";

- (id)initWithDictionary:(NSDictionary *)dict andUID:(int)uid {
    self.someEnum = [[dict objectForKey:value_type] integerValue];
    }

- (NSDictionary *)serializeToDictionary {
    dict[value_type] = [NSNumber numberWithInteger:self.someEnum];
}
该代码在swift 3中的等效性如何? 我发现在swift NSNumber中有init(value:)符号,但它只是初始化一个对象,而不是创建和初始化。init(value:)抛出一个错误,建议将“value”更改为“coder”。 我的Swift代码:

var someEnum = Enum.self

let value_type: NSString = "type"

 init(dictionary dict: NSDictionary, andUID uid: Int) {
    self.someEnum = dict.object(forKey: value_type) as! Enum.Type
}

 func serializeToDictionary() -> NSDictionary {
    dict[value_type] = NSNumber.init(value: self.someEnum)
}
Objective-C头文件:

typedef enum {
    EnumDefault = 0,
    EnumSecond = 1
} Enum;

static NSString *const value_type = @"type";

@property (nonatomic, assign) Enum someEnum;
目标C执行文件:

- (id)initWithDictionary:(NSDictionary *)dict andUID:(int)uid {
  if(self = [super init]) {
    self.someEnum = [[dict objectForKey:value_type] integerValue];
  }
  return self
}

- (NSDictionary *)serializeToDictionary {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[value_type] = [NSNumber numberWithInteger:self.someEnum];

    return dict;
}
someEnum
的值是一种类型,而不是特定的值。这是你的第一个错误

你可能想要类似的东西

var someEnum: Enum = ... // (your default value)
现在

枚举不会自动转换为整数。让我们假设
Enum
Int
值支持(并非所有Enum都是如此)。比您可以使用:

dict[value_type] = NSNumber(value: self.someEnum.rawValue)
或者只是

dict[value_type] = self.someEnum.rawValue as NSNumber
完整代码(在快速和异常状态下使用
NS(可变)字典不是一个好主意,我使用
解决了这些问题!
应该解决得更好)


我的枚举是Int类型。我尝试了您的代码,但它说-无法将“Class.Enum.RawValue.type”(又名Int.type)类型的值转换为中的NSNumber类型coercion@KasparsLapins您是否得到顶部的“first error”注释?Project使用此代码编译,但它是否执行相同的操作?dict[value\u type]=self.someEnum.rawValue()由于NSNumberMy enum有两个实例。我不确定如何在“first error”(第一个错误)注释中将这两个实例都放入某个enum变量中。因为如果我只声明默认值,则说明我不能在我要创建对象的行中使用RawValue成员。@KasparsLapins您能用完整的示例扩展代码吗?
dict[value_type] = NSNumber(value: self.someEnum.rawValue)
dict[value_type] = self.someEnum.rawValue as NSNumber
enum Enum : Int {
    case `default` = 0
    case second = 1
}

class Test {
    var someEnum: Enum = .default
    let valueType: String = "type"

    init(dictionary: NSDictionary, andUID uid: Int) {
        self.someEnum = Enum(rawValue: (dictionary[valueType] as! NSNumber).intValue) ?? .default
    }

    func serializeToDictionary() -> NSDictionary {
        let dictionary = NSMutableDictionary()
        dictionary[valueType] = self.someEnum.rawValue as NSNumber

        return dictionary
    }
}