Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 :-)