0

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()

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!

So, the question remains. If new() is there in NSObject, why does not compile? File Radar?

1
  • It does compile with Xcode 6.3 beta 4. Commented Apr 8, 2015 at 9:51

1 Answer 1

3

new is a reserved keyword and you have to escape it with backticks. This will work:

let string = NSString.`new`()

But I recommend using the "Swift" way as it shorter, cleaner and works for non NSObject kinds:

let string = NSString()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.