gpt4 book ai didi

ios - 快速替换 'Class'

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:11 26 4
gpt4 key购买 nike

我正在从 objective-C 转向 Swift。

头文件是:

@interface RegisterType : NSObject 
{
Class type;
NSString *typeName;
NSString *namespace;
int typeId;
}

@property (nonatomic,retain) Class type;
@property (nonatomic,retain) NSString *typeName;
@property (nonatomic,retain) NSString *namespace;
@property (nonatomic) int typeId;

@end

实现是:

@implementation RegisterType

@synthesize type;
@synthesize typeName;
@synthesize namespace;
@synthesize typeId;

现在是 Swift 我的代码是:

class RegisterType {

//var type: AnyClass
var typeName: String = ""
var namespace: String = ""
var typeId: Int = 0

}

我的主要问题是我想替换我在 Objective-C 中使用的泛型类型“Class”。如何在 Swift 中替换它?我需要一个初始化方法吗?

最佳答案

如果我正确理解你的问题,我会这样做:

class RegisterType: NSObject {

override init() {
super.init()
self.type = self.classForCoder
}

var type: AnyClass?
var typeName: String = ""
var namespace: String = ""
var typeId: Int = 0

}

let xx = RegisterType()
println(xx.type) // prints "RegisterType"

关于ios - 快速替换 'Class',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com