给出以下目标c代码
Class className = NSClassFromString(@"EKAttendee");
id attendee = [className new];
swift代码是什么?
答案 0 :(得分:0)
以下是一个简单示例,说明如何使用NSClassFromString
:
if let clazz = NSClassFromString("NSView") as? NSView.Type {
let v = clazz.init(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
print(v.frame) // (0.0, 0.0, 100.0, 100.0)
}
答案 1 :(得分:0)
尝试https://objectivec2swift.com/#/converter/code
我将其复制到其中
Class className = NSClassFromString(@"EKAttendee");
id attendee = [className new];
我回来了
var className: AnyClass = NSClassFromString("EKAttendee")
var attendee: AnyObject = className()
答案 2 :(得分:0)
这很有用。
let anyObjectType : AnyObject.Type = NSClassFromString("EKAttendee")!
let nsObjectType : NSObject.Type = anyObjectType as! NSObject.Type
var attendee: AnyObject = nsObjectType.init()
print("\(attendee)")
结果
EKAttendee <0x51763530>
email : (null)
isCurrentUser : 0
replyRequested : 0
role : (null)
status : (null)
type : 0
scheduleForceSend : 0