Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. import UIKit
  2.  
  3. /* THE BASICS S:1
  4. (Use ctrl + f 'S:(number)' to find a section)
  5. Intro to swift */
  6.  
  7. var str = "Hello, playground" //variable
  8. let int = 3 //Constant
  9.  
  10. var x = 3, y = 3, z = 3 //Multiple variable declaration
  11.  
  12. var testMessage: String //Proper way of setting a type for a variable
  13.  
  14. //Constants and variables can be pretty much anything except they can't start with numbers...
  15. //Also can't cast into a different type. E.g. String > Integer, like you can in Java.
  16.  
  17. print(x)
  18. let dog = "Brian" ; print(dog) //Can use semicolons but they are nor required unless using multi line code.
  19.  
  20.  
  21. let maxValue = UInt8.max //UInt8 is the 8bit version of the integer value. This is it's maximum value. Don't really need to use this unless required. Should do it automatically.
  22.  
  23. var unsignedInt :UInt32 ; unsignedInt = 35 //Declares and initializes an unsigned int of 32 bit size.
  24.  
  25. //Binary/Hexidecimal notation
  26. let decimalInteger = 17
  27. let binaryInteger = 0b10001
  28. let hexadecimalInteger = 0x34
  29.  
  30. //Exponent
  31. let exponent1 = 34e-2// means sqrt(exponent1)
  32.  
  33. /*Type conversion*/
  34. //Type conversion can be done on numbers using (type)variable like in java.
  35.  
  36. //Equivalent of typedef
  37. typealias AudioSample = UInt16
  38.  
  39. //Tuples
  40. let http404Error = (404, "Not Found")
  41. let http403Error = (403, "Definately not found")
  42. let (statusCode , statusMessage) = http404Error
  43. let (statusCode2, _) = http403Error //Only uses one section of the tuple
  44.  
  45. //Naming Individual elements at instantiation.
  46. let http402Error = (statuscode3 : 402, statusMessage2 : "So many errors!")
  47.  
  48. //Optionals...
  49. //ConvertedNumber is technically defined as the optional int? meaning it can either be an int or nothing. Can't be a String for example.
  50. let possibleNumber = "123"
  51. let convertedNumber = Int(possibleNumber)
  52.  
  53. //Nil is swifts equivalent of null except by default for e.g. defining a String as String? will default it too nil.
  54.  
  55. if let actualNumber = Int(possibleNumber){
  56. print("The String \"\(possibleNumber)\" has an integer value of \(actualNumber)")
  57. } else {
  58. print("The string \"\(possibleNumber)\" could not be converted to an integer")
  59. }
  60.  
  61. //implicit un-wrapped optionals. Basically use int! instead of int?. Makes damn sure we know exactly what type we are assigning.
  62.  
  63.  
  64. /*Error Handling*/
  65.  
  66. func MakeASandwich() throws {
  67. //...
  68. }
  69.  
  70. do {
  71. try MakeASandwich()
  72. //
  73. } catch {
  74.  
  75. }
  76.  
  77. /*Assertions and Preconditions
  78. - Assertions (Checked only in debug builds at runtime)
  79. - Preconditions (Checked in Debug and production builds)
  80. Really handy way of grabbing errors at runtime and stopping the program if this occours.*/
  81.  
  82. let age = -3
  83. //assert(age >= 0, "A person's age can't be less than 0.") //This assertion fails because -3 is less than 0.
  84.  
  85. //If the code is already checked. For example using an if statement we can use 'assertionFailure()' instead.
  86.  
  87.  
  88.  
  89.  
  90. /* BASIC OPERATORS - S:2 */
  91.  
  92.  
  93. //Pretty much all the same except Ternary Conditional Operators,
  94.  
  95.  
  96. /* ARRAYS + Multi line String + if/swift statements & while/for loops - 5.3 */
  97. let apples = "apple2.0"
  98.  
  99. let quotation = """
  100. this is a multi line quotation
  101. which is currently quoting an apple for some reason \(apples)"
  102. """"
  103.  
  104. var shoppingList = ["milk", "killer bees", "the number 42"]
  105. shoppigList[1] = "bottle of water"
  106.  
  107. var occupations = ["Malcolm": "Captain", "Nicolas": "Mechanic"]
  108. occuptations["Jayne"] = "public relations"
  109.  
  110. //Can also use .append, or create an empty array by doing the following
  111. let emptyArray = [String]()
  112. let emptyDictionary = [String: Int]()
  113. inferredList = [] //If type can be inferred
  114.  
  115. let vegetable = "red pepper"
  116.  
  117. switch vegetable {
  118. case "celery":
  119. print("ew!")
  120. case "cucumber", "watercress"
  121. print("slightly better")
  122. case let x where x.hasSuffix("pepper") //Checks if 'red pepper' has the suffic "pepper"
  123. print("Is is a spicy \(x)?")
  124. default:
  125. print("everything tastes good in soup.")
  126. }
  127.  
  128. let interestingNumbers = [
  129. "Prime": [2,3,5,7,11,13],
  130. "Fibonacci": [1,1,2,3,5,8],
  131. "Square": [1,4,9,16,25],
  132. ]
  133.  
  134. var largest = 0
  135. for (kind, numbers) in interestingNumbers {
  136. for number in numbers {
  137. if number > largest{
  138. largest = number
  139. }
  140. }
  141. }
  142.  
  143. print(largest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement