Swift Pages

Dot Net Perls has example pages for many languages, with explanations and code side by side (for easy understanding).

2D array.

Use 2D arrays: assign elements and loop over multidimensional arrays. See the syntax for 2D arrays.Swift 2D Array Examples


Use int and string arrays. Append to arrays, loop over elements and modify arrays.Swift Array Examples, String Arrays


Convert Strings to Ints and Ints to Strings. Handle invalid string data and optionals.Swift Convert String to Int Example


Use a dictionary to look up values from keys. Add elements and loop over tuples with for-in.Swift Dictionary Examples


Use Foundation, NSString and the contentsofFile argument to read a text file into a string.Swift File (Read Text File Into String)

Find, range.

Use range and contains to search for strings within other strings. Find a word in a string.Swift Find Strings: range of Example


Use the for-in loop and the enumerated method. See the while and repeat while loops.Swift Loops: for, repeat and while Examples


Use an if-else statement. Conditions in an if evaluate to true or false.Swift If, If Else Example

Inout, parameters.

Use the inout keyword for an argument in a func. Inout means an output parameter.Swift inout Keyword: Func Example


Use the Int type and the Int.max and Int.min properties. Test Ints with MemoryLayout.Swift Int Examples: Int.max, Int.min


Review keywords in the Swift language. These keywords are used throughout Swift programs.Swift Keywords

Lower, uppercase.

Use the lowercased and uppercased methods. Capitalize words with capitalized.Swift Lower, Uppercase and Capitalized Strings


Use math methods. Call abs, max, min, sqrt, floor, ceil and pow.Swift Math: abs, sqrt and pow


Use optional values. An optional may be nil, which means it has no value.Swift Optional: Nil Examples


Use the print method to display strings and other values to the console.Swift Print: Lines, Separator and Terminator


Store values with properties. Use get, set, lazy and the willSet and didSet observers.Swift Property Examples


Use ranges with startIndex and endIndex. Call the index func and specify offsetBy.Swift Range Examples (index, offsetBy)

Remove duplicates.

Use funcs, and the filter method, to remove duplicate elements from arrays.Swift Remove Duplicates From Array


Call replaceSubrange to replace strings. Use replacingOccurrences and replacingCharacters.Swift Replace String Example


Sort arrays with the sort method. Use funcs and closures to compare Strings and Ints.Swift Sort String Arrays: Closure Examples


Use components separatedBy to split a string into an array. Split with a string or characters.Swift Split Strings Example (components)

String length.

Get the length of a string with the endIndex property. Loop over the chars based on indexes.Swift String Length

String range, substring.

Get a substring from a string with a range. Use index, startIndex and offsetBy.Swift Substring Examples


Use strings and string literals. See a list of string methods that can be called.Swift String


Use the switch statement with cases and the default and fallthrough keywords.Swift Switch Statements: case, default and fallthrough

Try, catch.

Use the try and catch keywords to handle exceptions. Throw with a custom Error.Swift Error Handling: try, catch
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen,