Learn how to group data and functionality together in Swift, using a value type called structures.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3535-programming-in-swift/lessons/40
Learn how to group data and functionality together in Swift, using a value type called structures.
Hi,
When I first downloaded structure-final.playground, it looks like the code works perfectly but there is "Use of unresolved identifier ādistanceā on line 13. I guessed the cause of the problem and changed some codes but couldnāt solve the issue.
Please help me understand why this error appears.
Ah - sorry about that. I just checked and the problem is the distance()
function needs to be placed before the struct DeliveryArea
.
This is because Swift playground executes everything in the playground in order, and if you donāt declare the distance()
function before you try and use it inside DeliveryArea
, the playground doesnāt know the distance()
function exists.
I hope that helps, and let me know if you have any other questions! :]
Thanks for your quick reply. I copied all the codes except āimport Foundationā to inside viewDidLoad() in viewController.swift under newly created swift project. Then Xcode says āStruct declaration cannot close over value 'distance defined in outer scopeā on the same line.
I still donāt figure out whether it is a matter of playgroundās execution order.
Need help.
Remember that Playgrounds and Xcode projects work in slightly different ways.
Hereās an example of the code from the playground moved over to an Xcode project:
Struct.zip (27.7 KB)
viewDidLoad()
.I hope that helps!
Thank you so much. I learned a lot from your zipped code.
I have a question. If a structure is not mutably, then what is the point to use āvarā in a structure?
This topic was automatically closed after 166 days. New replies are no longer allowed.