6.2 Using extension to extend the method [Swift original tutorial]

Original: http://coolketang.com/staticCoding/5a99261eee920a671bb54427.html

1. This lesson will demonstrate extensions to methods for you. 



2. First import the interface tool framework to be used. 


3. Then define a structure with the specified name. 


4. Define two variables and set their default values. 


5. Then extend the structure object. 


6. Add an extension method to the struct that calculates the distance from another point. 


7. Obtain the horizontal and vertical distances between two points, respectively. 


8. Then returns the straight-line distance between the two points. Before first extending the method, first define a struct object. 


9. Before extending the method, first define a structure object. 


10. Next define another struct object. 


11. Calculate and return the distance between two points by calling the extension method of the structure object. From the real-time feedback area on the right, you can know the final calculation result. 


This article is organized from: "Swift4 Interactive Tutorial", the real [Hands-on] teaching mode, get started with iOS development and Swift language at the fastest speed, free download from Apple Store App Store: https://itunes.apple.com/cn/app/id1320746678 , or scan the QR code at the bottom of this page . Course supporting material download address: data download


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325896027&siteId=291194637