NSNumberオブジェクトを作成しようとしています。このコードは、SWIFT 3に相当しますどのようにSwift 3 NSNumberオブジェクトの作成
@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];
}
: 私はにObjCでこのコードを持っていますか? NSNumberにはinit(value:)というシンボルがありますが、オブジェクトを初期化して作成して初期化するのではないことがわかりました。 init(value:)はエラーを投げます。これは "value"を "coder"に変更することを示唆しています。 マイスウィフトコード:
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;
}
My EnumはInt型です。私はあなたのコードを試しましたが、それは言う - タイプ "Class.Enum.RawValue.Type(別名Int.Type)の型変換にNSNumberを変換することはできません –
@ KasparsLapinsあなたはトップに"最初のエラー "コメントを得ましたか? – Sulthan
プロジェクトはこのコードでコンパイルしますが、同じことをしますか?dict [value_type] = self.someEnum.rawValue()as NSNumber –