Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- var x = 3 // a mutable variable
- x = 4
- let y = 4 // an immutable constant
- //y = 5 // error because y is a let so we cannot change it
- var myInt:Int = 90 // explicit type declaration
- var myDouble:Double = 3.9
- var myFloat:Float
- // Int (and Int8, Int16, Int32, Int64), Double (and Float) , String, Bool, Character
- var z = x + y
- // we CANNOT implicitly preform math operations between different number types
- var addDouble = myDouble + Double(myInt)
- var addInt = Int(myDouble) + myInt
- typealias MyString = String
- let myString:MyString = "my string with typealias"
- typealias Long = Int64
- let myLong:Long = 69541235
- var num1,num2,num3:Int
- num1 = 4
- num2 = 5
- num3 = num1 * num2
- print("num3 is \(num3)") // string interpolation
- // class work
- // 1. define the radius of a cricle and calculate the circumference of the circle
- let radius:Double = 5
- var cir = 2 * Double.pi * radius
- print("the circumference is \(cir)")
- // 2. define 2 sides of a rectangle and calculate the area and parimeter
- let side1 = 9
- let side2 = 10
- let area = side1 * side2
- let per = 2 * (side1 + side2)
- print("the parimeter is \(per)")
- print("the eara is \(area)")
- // conditions
- if x < y {
- print("x is smaller than y")
- }
- else if x > y {
- print("x is greater than y")
- }
- else{
- print("x is equal to y")
- }
- switch myInt {
- case 0:
- print("myInt is 0")
- case 2:
- print("myInt is 2")
- case 3..<10:
- print("myInt is in 3..<10")
- default:
- print("myInt is not 2 and not 0")
- }
- let st = "hello"
- switch st {
- case "hi":
- print("hi")
- fallthrough // fallthrough makes the next case activate
- case "how are you":
- print("how are you?")
- default:
- print("I don't care")
- }
- // optionals
- // note that 'nil' is like 'null' in java
- //var myOptionalString:String? = nil
- var myOptionalString:String? = "the string"
- var myNonOptionalString = "the string"
- myNonOptionalString.append("A")
- print("non optional: \(myNonOptionalString)")
- myOptionalString?.append("A")
- print("optional: \(myOptionalString)")
- //myOptionalString!.append("A") // this is a force - unwrap. dangerous because we might have nil
- var anotherOptional:String! // we assume that when this variable is used that is has value (isn't nil). so then we don't need to use ? or !
- // like writing anotherOptional! everywhere we use it
- anotherOptional = "hi there"
- anotherOptional.append("A")
- if myOptionalString != nil {
- print("optional: \(myOptionalString!)")
- }
- // for this code to work it needs to be in a function
- //guard let safeValue = myOptionalString else{
- // print("myOptionalString is nil")
- // return // cannot have return in the global scop0e of the playground
- //}
- // loops
- // while and do-while are like java (we don't need parentheses)
- // for loops
- for i in 1..<10 { // numbers from 1 to 10 (NOT including 10)
- print(i)
- }
- for i in 1...10 { // numbers from 1 to 10 (including 10)
- print(i)
- }
- let numsArr = [10,20,30,40,50]
- for num in numsArr {
- print(num)
- }
- for index in numsArr.indices {
- print(numsArr[index])
- }
- let nameDict = ["key 1":"val 1","key 2":"val 2","key 3":"val 3"]
- for entry in nameDict{
- print("key \(entry.key), value \(entry.value)")
- }
- for key in nameDict.keys{
- print(key)
- }
- for value in nameDict.values{
- print(value)
- }
- for (key, value) in nameDict{
- print("key \(key), value \(value)")
- }
- for _ in 1..<5{ // _ is instead of a name for the variable since we don't need to use the variable
- print("in loop")
- }
- // collections
- // tuple
- var myTup = ("hello", 3)
- print(myTup.0)
- myTup.1 = 4
- var namedTup = (name:"the name", age:26, favFood:"Pizza")
- namedTup.name = "tzahi meaher"
- var tup:(char: Character, dub: Double)
- tup.char = "A"
- tup.dub = 3.9
- print(tup)
- // lists/array
- var myNumbers:[Int] = [] // java: int[] -> swift: [Int]
- var numsAgain = [Int]()
- var nums3 = Array<Int>()
- var numsAll0 = [Int](repeating:0, count:20) // makes an array with 20 elements all of them are set to 0
- var arrWithVals = [1,6,9,123,64] // like new int[] {1,6,9,123,64} in java
- print("is myNumbers empty? \(myNumbers.isEmpty)")
- myNumbers.append(36)
- myNumbers.append(99)
- myNumbers.append(14)
- myNumbers.append(6)
- print(myNumbers)
- print("number of elements in myNumbers \(myNumbers.count)")
- myNumbers.remove(at: 0) // remove the 0th element (first in the array)
- print(myNumbers)
- // remove by element
- let indexToRemove = myNumbers.index(where: { return $0 == 14 })
- let indexToRemove2 = myNumbers.index(where: { elem in
- return elem == 14
- })
- if indexToRemove != nil {
- myNumbers.remove(at: indexToRemove!)
- }
- print(myNumbers)
- myNumbers[0] = 9
- myNumbers.insert(102, at: 1) // insert a new element at a specific index
- print(myNumbers)
- // dictionary (like map in java)
- var myDictionary:[String:Character] = [:] // an empty dictionary the keys of type String and values of type Character
- var myDictionary2 = [String:Character]()
- var myDictionary3 = Dictionary<String, Character>()
- var dictWithVals:[String:Character] = ["key 1":"1", "key 2":"2", "key 3":"3"]
- print("elements in dictWithVals \(dictWithVals.count)")
- print("is myDictionary empty? \(myDictionary.isEmpty)")
- print(dictWithVals)
- dictWithVals["hello"] = "h" // inserting a new entery
- dictWithVals["key 1"] = "0" // updating an existing entry
- print(dictWithVals)
- dictWithVals["key 2"] = nil // removing an entry
- print(dictWithVals["nonsense"]) // the value of an entry by a key that does not exist is nil
- print(dictWithVals)
- // set
- var mySet = Set<String>()
- mySet.insert("hello")
- mySet.insert("hello1")
- mySet.insert("hello2")
- mySet.insert("hello") // will not be inserted since we already have "hello"
- print(mySet)
- print("number of elements in the set: \(mySet.count)")
- print("set contains hello?: \(mySet.contains("hello"))")
- // function
- func addNumbers(a:Int, b:Int)->Int{
- return a + b
- }
- addNumbers(a: 3, b: 5)
- func greetName(name:String){
- print("hello there \(name)")
- }
- greetName(name: "Mor")
- func subNumbers(a:Int = 0, b:Int = 0)->Int{ // default values for a and b
- return a - b
- }
- subNumbers(a:89)
- func greet(nameToGreet name:String){
- print("hello there \(name)")
- }
- greet(nameToGreet: "Daniel")
- func sumToNum(_ num:Int)->Int{
- var sum = 0
- for elem in 0...num {
- sum += elem
- }
- return sum
- }
- sumToNum(6)
- func getNumbersAsTuple(num1: Int, num2:Int)->(a:Int, b:Int){
- return (a:num1,b:num2)
- }
- let myNumsAsTup = getNumbersAsTuple(num1: 1, num2: 2)
- print("a: \(myNumsAsTup.a), b: \(myNumsAsTup.b)")
- let myFuncVar = getNumbersAsTuple
- let myFuncVar2:(String,String)->(Int) = { st,st2 in
- return st.count + st2.count
- }
- let myFuncVar3:(String,String)->(Int) = {
- return $0.count + $1.count // $0 is the first argument, $1 is the second argument
- }
- print(myFuncVar(96, 5))
- print(myFuncVar2("hello there","how are you"))
- func doMath(num1:Int, num2:Int, action:(Int,Int)->Int) -> Int{ // this function gets a function as an argument. the function is gets has two arguments - Int and Int- and it returns an Int
- return action(num1, num2)
- }
- let resPlus = doMath(num1: 3, num2: 9, action: { num1, num2 in
- return num1 + num2
- })
- let resStangeMath = doMath(num1: 3, num2: 9, action: {
- return ($0 * $1) % 2
- })
- print(resPlus)
- print(resStangeMath)
- func makeMultFunc(multiplier:Int) -> (Int)->Int { // functio that returns a function (the function that is returns gets an Int as an argument and returns an Int)
- return { num in
- return multiplier * num
- }
- }
- let mult2Func = makeMultFunc(multiplier: 2) // this is a function
- mult2Func(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement