![]() ![]() """ // Arrays let shoppingList = //commas are allowed after the last element let secondElement = shoppingList // Arrays are 0-indexed // Arrays declared with let are immutable the following line throws a compile-time error //shoppingList = "mango" // Arrays are structs (more on that later), so this creates a copy instead of referencing the same object var mutableShoppingList = shoppingList mutableShoppingList = "mango" // = is equality shoppingList = mutableShoppingList // false // Dictionaries declared with let are also immutable var occupations = occupations = "Public Relations" // Dictionaries are also structs, so this also creates a copy let immutableOccupations = occupations immutableOccupations = occupations // true // Arrays and dictionaries both automatically grow as you add elements mutableShoppingList. You can include " or "" in multi - line strings because the delimiter is three "s. It's called that because it takes up multiple lines (wow!) Any indentation beyond the closing quotation marks is kept, the rest is discarded. let multiLineString = """ This is a multi-line string. ![]() They also change the escape character // to a backslash followed by the same number of number signs. let equation = "Six by nine is \( 6 * 9 ), not 42!" // To avoid escaping double quotes and backslashes, change the string delimiter let explanationString = # "The string I used was " The value of aDouble is \ ( aDouble ) " and the result was \#(descriptionString)" # // You can put as many number signs as you want before the opening quote, // just match them at the ending quote. let stringWithDouble = aString + String ( aDouble ) let intFromDouble = Int ( aDouble ) // For strings, use string interpolation let descriptionString = "The value of aDouble is \( aDouble ) " // You can put any expression inside string interpolation. Explicitly make instances of the desired type. let aString : String = "A string" let aDouble : Double = 0 // Values are never implicitly converted to another type. To explicitly declare the type, write it after the variable name, // separated by a colon. let theAnswer = 42 var theQuestion = "What is the Answer?" theQuestion = "How many roads must a man walk down?" theQuestion = "What is six by nine?" // Atttempting to reassign a constant throws a compile-time error //theAnswer = 54 // Both variables and constants can be declared before they are given a value, // but must be given a value before they are used let someConstant : Int var someVariable : String // These lines will throw errors: //print(someConstant) //print(someVariable) someConstant = 0 someVariable = "0" // These lines are now valid: print ( someConstant ) print ( someVariable ) // As you can see above, variable types are automatically inferred. print ( "Hello, world" ) // // MARK: - Variables // //Use `let` to declare a constant and `var` to declare a variable. import a module import Foundation // Single-line comments are prefixed with // // Multi-line comments start with /* and end with */ /* Nested multiline comments /* ARE */ allowed */ // Xcode supports landmarks to annotate your code and lists them in the jump bar // MARK: Section mark // MARK: - Section mark with a separator line // TODO: Do something soon // FIXME: Fix this code // MARK: Hello, World // From Swift 3 on, to print, just use the `print` method.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |