Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- /* THE BASICS S:1
- (Use ctrl + f 'S:(number)' to find a section)
- Intro to swift */
- var str = "Hello, playground" //variable
- let int = 3 //Constant
- var x = 3, y = 3, z = 3 //Multiple variable declaration
- var testMessage: String //Proper way of setting a type for a variable
- //Constants and variables can be pretty much anything except they can't start with numbers...
- //Also can't cast into a different type. E.g. String > Integer, like you can in Java.
- print(x)
- let dog = "Brian" ; print(dog) //Can use semicolons but they are nor required unless using multi line code.
- 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.
- var unsignedInt :UInt32 ; unsignedInt = 35 //Declares and initializes an unsigned int of 32 bit size.
- //Binary/Hexidecimal notation
- let decimalInteger = 17
- let binaryInteger = 0b10001
- let hexadecimalInteger = 0x34
- //Exponent
- let exponent1 = 34e-2// means sqrt(exponent1)
- /*Type conversion*/
- //Type conversion can be done on numbers using (type)variable like in java.
- //Equivalent of typedef
- typealias AudioSample = UInt16
- //Tuples
- let http404Error = (404, "Not Found")
- let http403Error = (403, "Definately not found")
- let (statusCode , statusMessage) = http404Error
- let (statusCode2, _) = http403Error //Only uses one section of the tuple
- //Naming Individual elements at instantiation.
- let http402Error = (statuscode3 : 402, statusMessage2 : "So many errors!")
- //Optionals...
- //ConvertedNumber is technically defined as the optional int? meaning it can either be an int or nothing. Can't be a String for example.
- let possibleNumber = "123"
- let convertedNumber = Int(possibleNumber)
- //Nil is swifts equivalent of null except by default for e.g. defining a String as String? will default it too nil.
- if let actualNumber = Int(possibleNumber){
- print("The String \"\(possibleNumber)\" has an integer value of \(actualNumber)")
- } else {
- print("The string \"\(possibleNumber)\" could not be converted to an integer")
- }
- //implicit un-wrapped optionals. Basically use int! instead of int?. Makes damn sure we know exactly what type we are assigning.
- /*Error Handling*/
- func MakeASandwich() throws {
- //...
- }
- do {
- try MakeASandwich()
- //
- } catch {
- }
- /*Assertions and Preconditions
- - Assertions (Checked only in debug builds at runtime)
- - Preconditions (Checked in Debug and production builds)
- Really handy way of grabbing errors at runtime and stopping the program if this occours.*/
- let age = -3
- //assert(age >= 0, "A person's age can't be less than 0.") //This assertion fails because -3 is less than 0.
- //If the code is already checked. For example using an if statement we can use 'assertionFailure()' instead.
- /* BASIC OPERATORS - S:2 */
- //Pretty much all the same except Ternary Conditional Operators,
- /* ARRAYS + Multi line String + if/swift statements & while/for loops - 5.3 */
- let apples = "apple2.0"
- let quotation = """
- this is a multi line quotation
- which is currently quoting an apple for some reason \(apples)"
- """"
- var shoppingList = ["milk", "killer bees", "the number 42"]
- shoppigList[1] = "bottle of water"
- var occupations = ["Malcolm": "Captain", "Nicolas": "Mechanic"]
- occuptations["Jayne"] = "public relations"
- //Can also use .append, or create an empty array by doing the following
- let emptyArray = [String]()
- let emptyDictionary = [String: Int]()
- inferredList = [] //If type can be inferred
- let vegetable = "red pepper"
- switch vegetable {
- case "celery":
- print("ew!")
- case "cucumber", "watercress"
- print("slightly better")
- case let x where x.hasSuffix("pepper") //Checks if 'red pepper' has the suffic "pepper"
- print("Is is a spicy \(x)?")
- default:
- print("everything tastes good in soup.")
- }
- let interestingNumbers = [
- "Prime": [2,3,5,7,11,13],
- "Fibonacci": [1,1,2,3,5,8],
- "Square": [1,4,9,16,25],
- ]
- var largest = 0
- for (kind, numbers) in interestingNumbers {
- for number in numbers {
- if number > largest{
- largest = number
- }
- }
- }
- print(largest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement