Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Classes and Objects Classes and Their Methods

sir may i know why we can not access Product.discountedPrice(percentage: double)) outside the class

sir may i know why we can not access Product.discountedPrice(percentage: double)) outside the class when i use product. so it auto complete by product.discountedPrice(self: Product) is it necessary to access the discountedPrice function through the new variable.

2 Answers

Hi There,

Obviously I don't know what you code looks like so I can only give you a rough example using a variable, you'll have to move this around to get it to work with your class.

The reason you can't access your

Product.discountedPrice(percentage: double))

outside your class is because you declared your variable within your class, Swift is really kind of pedantic about that kind of thing so you would be best declaring a global variable lets say declare something like this at the start of viewController or project.

View Contoller.Swift 
var discountedPrice : [Int] = 0 // if its an integer
or 
var discountedPrice : [String] = "" //  If on the off chance your using a string 

and then in the class you should call the variable by using

 discountedprice = 0 // instead of redefining it again with var

Hope this helps.

Thank you Sir, i got it

Excellent, I'm glad I could help :-)