0

So what I wanna do is:

let margin: CGFloat = 10
let width: CGFloat = 100 - margin
// also used self.margin

but here's the error I'm getting:

Instance member 'margin' cannot be used on type 'HistoryViewController'

What should I do?

3 Answers 3

4
let margin: CGFloat = 10
var width: CGFloat { return 100 - self.margin }

Or

lazy var width: CGFloat = 100 - self.margin

This is because at instance variable initialization time, the instance doesn't exist yet. Using lazy initialization or a computed property will fix this.

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

1 Comment

Thanks both solutions worked. I chose the first one.
1

You can do it like this:

first declare the class level variables like this:

let margin: CGFloat = 10
var width = CGFloat()

Then give your width a value inside a function, say viewDidLoad() like this:

override func viewDidLoad() {
        super.viewDidLoad()
        width = 100 - margin
    } 

Thank You!
Happy Coding

Comments

1
var margin: CGFloat {
  return 10.0
}
var width: CGFloat {
  return 100 - margin
}

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.