5.16 Use Any to represent arbitrary value types [Swift original tutorial]

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

1. AnyObject can represent an instance of any class type. There is also an Any, which can represent any type, except method types. This lesson will show you how to use Any. 



2. Define an array of type Any, which can load various types of data. 


3. Add four types of data to the array in turn: integer, floating-point, string, and tuple. 


4. By entering the name of the array, you can view all the contents of the array from the real-time feedback area on the right. Click the display result icon on the right to view all elements in the array in the result list area. 


5. Then click the icon here again to hide the result list. 


6. Continue writing code to traverse the array of composite types and output the value of each element. 


7. First create a loop statement. 


8. Then, perform conditional judgment on the traversed elements. 


9. Through the let-as operator, try to convert the element to an integer, and output the corresponding log in the console. 


10. Then try to convert the element to double-precision floating point type, and output the corresponding log in the console. 


11. Convert the element to string type and output the corresponding log on the console. 


12. Convert the element to a tuple type and output the value of each element in the tuple to the console. 


13. Finally, deal with the cases under other conditions uniformly. Then click the show debug area icon here, open the console, and view the output log. 


14. 



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=325379587&siteId=291194637