0

I have a class in Obj-C, it can only be initialized by calling +new, and -init is not supported:

@interface SetupMainController : UIViewController
+(SetupMainController *)new;
-(id)init __attribute__((unavailable("Must use +new")));
@end

I am trying to run the following equivalent obj-c code in swift:

SetupMainController *setupController = [SetupMainController new];
[self presentViewController:setupController animated:YES completion:nil];

like so:

let sc : SparkSetupMainController = SparkSetupMainController.new()

or:

let sc : SparkSetupMainController.new()

or:

let sc : SparkSetupMainController()

(which obviously tries to call -init which is prohibited) all fails, getting "expected member name following '.'" error.

I found answers like this or this or apple docs but none give a straight answer how to do that simple task in Swift.

Help appriciated

10
  • 1
    Why do you want to do this? Calling new is jus calling alloc init. There doesn't seem to be a reason to make init unavailable. Commented Apr 8, 2015 at 6:13
  • because implementation of +new() is doing this: return [[SetupMainController getSetupStoryboard] instantiateViewControllerWithIdentifier:@"root"]; function getSetupStoryboard is a class method as well, loading the relevant storyboard from the bundle Commented Apr 8, 2015 at 6:23
  • You should not have that in new. You should have a method called something like setupViewControllerFromStoryboard. Commented Apr 8, 2015 at 6:29
  • ok, let's say the class method is called setupViewControllerFromStoryboard(), how do I call that from swift? Commented Apr 8, 2015 at 6:30
  • 1
    Yes I think so. I was surprised that you were able to use new at all. Normally Xcode will shout at you for using a method name beginning with new. Commented Apr 8, 2015 at 6:35

1 Answer 1

1

If you change newin your Objective-C class to:

+(id)mynew;

Then suddenly all works:

    var s: SetupMainController = SetupMainController.mynew()

Don't know why you cannot override +new. Maybe it's because is a class method?

EDIT: To add more mystery to this question, if you look up new in Apple's doc is defined in Swift using:

class func `new`() -> Self!

Do the backticks mean something like "this is reserved"?

EDIT 2: Looks like new doesn't work at all with Swift

This code does not compile:

let j = NSNumber.new()
var s = NSString.new()

EDIT 3:: SOLUTION

new is a reserved keyword in Swift, so to call +new() you need to add the back ticks:

let string = NSString.`new`()

Kudos to this answer

Sign up to request clarification or add additional context in comments.

3 Comments

lovely! didn't know that about the backticks, nor that new is reserved. decided to resolve to using normal -init() in the SetupMainController Obj-c class my question, is this ok to do? -(instancetype)init { return [[SetupMainController getSetupStoryboard] instantiateViewControllerWithIdentifier:@"root"]; } (not calling [super init] at any point)
Yes, but looks a little fragile: you depend on having a VC with a "root" identifier. Better create two inits, one initWithIdentifier: that gets an NSString and one init that call the designated initWithIdentifier passing "root"
thing is I do not want to expose initWithIdentifier to user since this viewController must be instantiated only from the "root" storyboard instance, otherwise return nil. (so cannot mark it as NS_DESIGNATED_INITIALIZER anyways)

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.