0

How to implement the below objective-c init method in swift

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withName:(NSString *)name {
    if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.name = name;
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    }
    return self;
}
3
  • themainthread.com/blog/2014/08/… Commented Oct 13, 2014 at 12:24
  • @TonyMkenu I got something from the link.I just want to know is it possible to send parameters like or should i follow some other way. Commented Oct 13, 2014 at 12:46
  • 1
    required init(coder aDecoder: NSCoder!) { foo = "some string" bar = 9001 super.init(coder: aDecoder) } Commented Oct 13, 2014 at 12:56

1 Answer 1

1

The equivalent would be this:

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?, name:String!)
{
      self.init(nibName: nibNameOrNil, bundle: nibBundleOrNil);

        self.name = name;
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "bg.png"));
}

You may have to implement required initializers if you get an error, but should not be needed if you use a later version than Beta5:

required init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)
}
Sign up to request clarification or add additional context in comments.

4 Comments

where the sending of "name" parameter
Sorry, forgot that one. I added a it as parameter. But, as in ObjC, now it is not a standard initializer anymore.
then how to call this method from a another viewController. I mean how to pass data from one controller to another controller.
Same way as you would do it in ObjC, you define the function in your header and use it instead of the standard initializers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.