1

In my code I'm getting:

missing argument for parameter 'coder' in call swift

I'm a beginner to Swift and I've already tried everything including researching this question, but have found no answer. Thanks.

The code I'm getting this error is:

let button: UIButton = UIButton(frame: CGRect(origin: CGPoint(x: ChecklistViewController().view.frame.width / 2 + 117, y: ChecklistViewController().view.frame.size.height - 70), size: CGSize(width: 50, height: 50)))

2
  • 3
    You should edit the question to share the line of code that is generating this error. Commented Aug 13, 2016 at 21:48
  • Alright, I updated the question. Commented Aug 13, 2016 at 22:07

2 Answers 2

3

The error is suggesting that the compiler is having trouble figuring out which init method should be invoked, so it's assuming you meant to call init(coder:).

But let's set that aside for a second. First, let's simplify your statement to eliminate some "noise". Rather than using CGRect(origin:, size:), you might use CGRect(x:, y:, width:, height:). That would yield (separating it on separate lines to make it a little easier to read):

let button = UIButton(frame: CGRect(
    x: ChecklistViewController().view.frame.width / 2 + 117,
    y: ChecklistViewController().view.frame.size.height - 70,
    width: 50,
    height: 50)
)

Second, the problem here is that that ChecklistViewController() syntax isn't actually referencing your existing ChecklistViewController. Every time it sees ChecklistViewController() it's creating a new instance of that view controller (so you probably have three instances, the original one and the two you accidentally created here). That's certainly not what you intended. If you were doing this in, one of the instance methods of the view controller itself, you'd just reference self, e.g.:

let button = UIButton(frame: CGRect(
    x: self.view.frame.width / 2 + 117,
    y: self.view.frame.size.height - 70,
    width: 50,
    height: 50)
)

A more subtle problem is that this code will only work if the frame of the view has been set. But if you have this code in viewDidLoad, the frame hasn't been set yet. If you did this in viewDidAppear, though, you could get away with this code. Generally, you'd use auto layout, to avoid this, e.g. something like:

let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
// do additional configuration of the button here

view.addSubview(button)

NSLayoutConstraint.activateConstraints([
    button.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor, constant: 117),
    button.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor, constant: -70),
    button.widthAnchor.constraintEqualToConstant(50),
    button.heightAnchor.constraintEqualToConstant(50)
])

Because we did this with auto layout, it means you can do this in viewDidLoad, if you want. Also, it means that if you rotate the device, the constraints will recalculate the frame automatically for you.

Having said all of this, the "missing argument for parameter 'coder'" might be a result of some other problem in the code. But, if you fix the declaration of the button, you might be able to better diagnose any other issue that might be in the code.

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

2 Comments

Thank you for your help, but when I used "self" in the code instead of ChecklistViewController(), I got a different error. This error said: Value of type 'NSObject -> () -> ChecklistViewController' has no member of 'view'
You'd only use self if you were doing this from an instance method of the view controller. It doesn't sound like that's where you're doing this, but without more context we can't help. I.e., where do you have the code from your question? But bottom line, don't use CheckViewController() syntax, but rather get a reference to the existing instance.
0

I think the problem is that you are using ChecklistViewController to generate your positions. Try this code

let button: UIButton = UIButton(frame: CGRect(x: (CGRectGetMidX(self.view.frame) + 117) , y: (CGRectGetMaxY(self.view.frame) - 70) , size: CGSize(width: 50, height: 50)))

2 Comments

When I tried that I got an error saying: Value of type 'NSObject -> () -> ChecklistViewController' has no member 'view'
Are you making this in a viewController? And it need to be located within a class.

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.