20 runnable Swift code examples.
Open any Swift example to explore the code and run it online. No setup, no signup.Explore practical Swift code examples with clear explanations. Open any example to edit and run it online β no setup, no signup.
Swift
20How to loop with an index in SwiftHow to get the length of a string in SwiftHow to split a string in SwiftHow to convert a string to an int in SwiftHow to check if a string contains a substring in SwiftHow to sort an array of objects by property in SwiftHow to unwrap an optional in SwiftHow to get a substring in SwiftHow to round to 2 decimal places in SwiftHow to generate a random number in a range in SwiftHow to loop through an enum in SwiftHow to replace all occurrences in a string in SwiftHow to check if an array contains a value in SwiftHow to concatenate two arrays in SwiftHow to trim whitespace from a string in SwiftHow to handle errors in SwiftHow to parse JSON into a struct in SwiftHow to format a date in SwiftHow to calculate the difference between two dates in SwiftHow to sleep for milliseconds in Swift
Questions & answers
Swift examples, explained
Can I run these Swift code examples?
Yes. Open any Swift example to edit and run its code on the CompileBytes engine without installing anything.
Is this free? Do I need an account?
Every example is free to read, run, edit, and copy. No account is required.
Can I copy these into my own projects?
Yes. These examples are designed to be copied and adapted to your own code.
Where can I run my own code?
Open the Swift compiler from the main playground catalog, then paste or write any code you want to run.