在Objective-C中,我不是new的忠实拥护者.但是这个问题提出了疑问.为什么不能编译?:
I wasn't a big fan of new in Objective-C. But this question raised the doubt. Why this does not compile?:
let j = NSNumber.new() var s = NSString.new()看起来像new()是常规的类方法.尽管在Apple的文档中,在名称??
Looks like new() is a regular class method. Although in Apple's doc is defined with back ticks around the name ??
class func `new`() -> Self!因此,问题仍然存在.如果NSObject中存在new(),为什么不进行编译?文件雷达?
So, the question remains. If new() is there in NSObject, why does not compile? File Radar?
推荐答案new是保留关键字,您必须使用反引号将其转义.这将起作用:
new is a reserved keyword and you have to escape it with backticks. This will work:
let string = NSString.`new`()但是我建议使用快速"方式,因为它更短,更干净并且适用于非NSObject类型:
But I recommend using the "Swift" way as it shorter, cleaner and works for non NSObject kinds:
let string = NSString()更多推荐
为什么使用new()类方法在Swift中创建对象无法编译?
发布评论