Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
}
share|improve this question

1 Answer 1

The equivalent would be this:

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?, name:String!)
{
        super.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)
}
share
    
where the sending of "name" parameter –  sateesh 2 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.