User:Bn335/sandbox/swift

Use this code to add an article from wiki arab:

Also see bonus to some lessons at swift book - Bonuses

Introduction
Our instructior journey:
 * March2015 coffeshop working and Online learning of Html&Css for a month
 * April2015 Swift Online free-courses
 * June-sept2015 Bootcamp
 * Oct2015 first simple app
 * Nove2015 Haired first job for 60k a year
 * Whole2016 coding programing developing designing - 100k a year
 * May2017 starting youtube channel
 * April2018-Jul2019 Aluna company
 * Aug2019 Full time making youtube videos as expert but not accepted to work in FAANG and even he think didn't good in the screen interview because it's hard and need to be extra-expert. (not so easy to be FAANG level)

معاني كلمات

 * repository (تلفظ: ريبو زتو ري) مستودع
 * مستودع برمجيات
 * Git Repository Creation Failed Ensure the author information supplied in Xcode:settings is correct then create the git repository using Source control:NewGitRepository
 * canva = لوح قماشي

ما قبل الدرس الاول

 * you need a Mac. then download xcode from the appstore
 * Xcode is IDE (Integrated Development Environment)
 * Xcode v11-June2021 v14.1-Dec2022
 * SwiftUi vs UiKit ichoseUIKIT

ملفات المجلد

 * FFFFFF.xcodeproj:
 * -1-
 * FFFFFF:
 * FFFFFFApp.swift
 * ContentView.swift
 * Assets.xcassets
 * FFFFFF.entitlements
 * -1c1-
 * Preview Content:
 * Preview Assets.xcassets
 * Preview Content:
 * Preview Assets.xcassets

Lesson 1

 * // var (لفظ يستعمل للتعريف بـ مُستقبِل)
 * // let (لفظ للتعريف بـ ثابت) constant
 * // Types of variables: انواع المُستقبِلات
 * // Int, Double, Float, String, Bool, Any


 * // this is a comment, for multiple lines you can use this /* */. you can use keyboard shortcut command+/ (no selection needed)
 * var a = "Hello Bn\nhi\n" // \n is like enter
 * print(a)


 * //المُستقبِلات
 * var vNuma:Int = 21
 * var vNumb = 21
 * var vNamea:String = "Bn"
 * var vNameb = "Bn"
 * var vPrcent:Double = 0.76
 * var visDarkModeOn:Bool = true
 * //(افضل اضافة حرف "في" قبل احرف المستقبِل علمًا انه بالمحررات الحديثة يمكن تلوين المُستقبِلات بلون موحد لتسهيل تميزها)


 * //مصفوفة Array (list of items) index رقم لكل خلية
 * var vLista:[Int] = [21, 55]
 * var vListc:[Int] = []
 * var vListb = [21, 55, 19, 22, 22, 37, 88, 71]


 * //فعاليات بين المُستقبلات والمصفوفات
 * vLista = vListb
 * vNuma = vLista[0]
 * vNumb = vLista.count
 * var vNumc = vLista.first
 * var vNumd = vLista.last
 * vLista.append(55) // يضيف القيمة الى اخر المصفوفة
 * vLista.insert(44, at: 0) // يضيف القيمة قبل في الاندكس ويزيح السابقات
 * vLista.reverse
 * vLista.shuffle


 * //حزمة Set
 * var vSeta:Set = [21, 66]
 * var vSetn = [21, 55]
 * var vSetc:Set = []
 * var vSetd:Set = Set(vLista)
 * var vSete = Set(vLista)
 * vSeta.contains(17)
 * // كما تلاحظ سيتن هو ليس ست بل مصفوفة


 * //Dictionary قاموس Key Value
 * var vDictn:[String] = []
 * var vDicta:[String:String] = [:]
 * var vDictb:[String:String] = [
 * "Phone": "iPhone14",
 * "Tablet": "MacBookPro13",
 * "Laptop": "iPad Air 2021",
 * "Desktop": "Lenovo",
 * "Car": "BMW",
 * "Location": "Dubai"
 * ]
 * // كما تلاحظ ديكتن ليس قاموس بل مصفوفة


 * //فعاليات
 * print(vDictb)
 * print(vDictb["Car"])
 * print(vDictb["Car"] ?? "Nope" as Any)
 * var vNume = vDictb.count
 * var vNamec = vDictb["Laptop"]
 * vDictb["Laptop"] = "MacBookPro14"
 * vDictb["Tablet"] = vNamec
 * print(vDictb)
 * vDictb["Name"] = "Bn" //Adding Value and Key
 * vDictb.removeValue(forKey: "Location") // removing Value and Key
 * vDictb["Tablet"] = "" // Emptying the Value
 * print(vDictb)

Lesson 2
// دوائر التكرار (Conditionals) (if.. else.. else if..) (while..) (repeat while..) (for.. in..)

//Conditionals الجمل الشرطية
 * //Equal to (a == b)
 * //Not equal to (a != b)
 * //Greater than (a > b)
 * //Less than (a < b)
 * //Greater than or equal to (a >= b)
 * //Less than or equal to (a <= b)

//if.. else..
 * var a = 5, b = 6
 * if a == b {
 * print("a is b")
 * } else {
 * print("nope, a is not b")
 * }

//if.. else.. else if..
 * var a = 90, b = 80, c = 70, d = 60, f = 50
 * var vGrade = 58
 * if vGrade >= a {
 * print("A++")
 * } else if vGrade >= b {
 * print("A+")
 * } else if vGrade >= d {
 * print("A")
 * } else {
 * print("B")
 * }

//while
 * var vLista = ["Phone", "Tablet", "Laptop", "Desktop", "Car", "Location"]
 * var i = 0
 * while i < vLista.count {
 * print(vLista[i])
 * i = i + 1 // i+=1
 * }
 * print("while Loop Ended\n")

//repeat
 * i = 0
 * repeat {
 * print(vLista[i])
 * i = i + 1
 * } while i < vLista.count
 * print("repeat Loop Ended")

//for.. in..
 * var vLista = ["Phone", "Tablet", "Laptop", "Desktop", "Car", "Location"]
 * for i in vLista { //here you note that with for loops, there is no need to "var the variable"
 * print(i)
 * }
 * for i in 1...10 {
 * print(i)
 * }
 * for i in 0...vLista.count-1{
 * print(vLista[i])
 * }
 * for i in vLista { // في المصفوفات الافضل دائما استخدام هذه الصيغة
 * print(i)
 * }
 * var vDicta:[String:Any] = ["Name":"Bn", "Age":30, "Address":"Dubai"]
 * for i in vDicta.keys {
 * print(vDicta[i])
 * }
 * for i in vDicta.keys {
 * print("\(i) has value: \(vDicta[i]!)")
 * }
 * }

Lesson 3
//Initials قيم ابتدائية
 * var a:Int = 9

//Optionals (قيم غير اكيدة المنحى) قيم اختيارية
 * // Optionals cases topic, let's look at these phraps:
 * var vLista = [21, 55, 19, 22, 22, 37, 88, 71]
 * var vMuna = vLista[1]
 * print (vMuna) //output: 55
 * var vNuma = vLista.last
 * print(vNuma) //output: Optional(71)
 * var vListb:[Int] = [21, 55, 19, 22, 22, 37, 88, 71]
 * var vNumb = vListb.last
 * print(vNumb) //output: Optional(71)
 * var vListc:[Int] = [21, 55, 19, 22, 22, 37, 88, 71]
 * let vNumc = vListc.last
 * print(vNumc) //output: Optional(71)
 * var vListd:[Int] = []
 * var vNumd = vListd.last
 * print(vNumd) //output: nil
 * // you as dev you have to "unwrap the optional" (absence of a value cases)
 * // so how to unwrap the optionals? there is 4 ways
 * // 1. if else method
 * // 2. nill coalescing method
 * // 3. func guard statement
 * // 4. force unwrap (not recommended)
 * // Unwraping the optional of the previus cases:
 * // 1. if else method
 * var vListe:[Int] = []
 * if var vNume = vListe.last {
 * print(vNume)
 * }else {
 * print("There is no entry")
 * }//output: There is no entry
 * // 2. nil coalescing method
 * var vLis:[Int] = []
 * var vNu = vLis.last ?? 55
 * print(vNu) //output: 55
 * // 3. func guard statement
 * var vLisa:[Int] = [77, 55]
 * vLisa.sort // note: we use this to sort the array so the last is largest number
 * func vwhatsMaxLisa {
 * guard var vNua = vLisa.last else{
 * print("There is no entry")
 * return // return will stop the func and not do the next
 * }// do the next (if yes there is a value) :
 * // a code here...
 * print(vNua) //output: 55
 * }
 * vwhatsMaxLisa
 * // 4. force unwrap (not recommended)
 * // that's mean, force use it even if it nil, that's so not good. use ?? to have a value instead.
 * var vLisb:[Int] = [71, 55]
 * var vNub = vLisb.last!
 * print(vNub) //output: 55
 * var vLisc:[Int] = []
 * //var vNuc = vLisc.last! //
 * //print(vNuc) // output: Fatal error
 * //Conclusion, The best and easiest way is method 2
 * // 4. force unwrap (not recommended)
 * // that's mean, force use it even if it nil, that's so not good. use ?? to have a value instead.
 * var vLisb:[Int] = [71, 55]
 * var vNub = vLisb.last!
 * print(vNub) //output: 55
 * var vLisc:[Int] = []
 * //var vNuc = vLisc.last! //
 * //print(vNuc) // output: Fatal error
 * //Conclusion, The best and easiest way is method 2
 * //Conclusion, The best and easiest way is method 2
 * //Conclusion, The best and easiest way is method 2

//More cases مثال اخر ينتج به اوبشينال ونقوم بتعديله
 * var a:Int = 5
 * print(a) // output: 5
 * //case
 * var b:Int? // معنى علامة سؤال واحدة هو اني لا اريد ادخال قيمة ابتدائية وعندها ستكون قيمته نيل ولها استعمالات
 * b = 4
 * print(b) // output: Optional(4)
 * var c:Int?
 * c = 3
 * print(c ?? 0) //this will look, if nil, convert it to a value using ?? VALUE HERE // output: 3
 * var c:Int?
 * c = 3
 * print(c ?? 0) //this will look, if nil, convert it to a value using ?? VALUE HERE // output: 3

- //Casting تحويل نوع مُستقبِل الى نوع مستقبل اخر
 * var c:String = "456"
 * var d:Int = Int(c) ?? 1 // تحويل سترينج الى ارقام واذا فارغة او بها نصوص فاكتب رقم واحد
 * print(d) // output: 456
 * var a:String = "1234" //Another way (less recommended)
 * var b:Int = Int(a)!
 * print(b) // output: 1234
 * var e:String = ""
 * var f:Int = Int(e) ?? 1
 * print(f) // output: 1
 * //Bonus: printing number within strings, converting numbers to string
 * var j:Int = 789
 * var k = "Hi \(j)" //String
 * print(k)
 * var u = "\(j)" // تحويل رقم الى سترينج
 * u = "Hello"
 * print(u) // string
 * var k = "Hi \(j)" //String
 * print(k)
 * var u = "\(j)" // تحويل رقم الى سترينج
 * u = "Hello"
 * print(u) // string
 * print(u) // string

//Tuples وهي ادخال اكثر من قيمة للمستقبل، يشبه الى حد ما المصفوف ولكن لكل قيمة يمكن ان يكون نوع مختلف
 * var p = (555, "Heloo")
 * print(p) // output: (555, "Heloo")
 * print(p.1) // output: Heloo
 * var (t,r) = p
 * t = t + 1
 * print(t) // output: 556

Lesson 4
//Full function be like:
 * func a(b c:String) -> String
 * let w:String = "be \(c)" // why let not var
 * return w
 * }
 * var a:String = a(b: "bh")
 * print(a)
 * print(a)

//Simplest func be like:
 * func a{
 * print("hi")
 * }
 * a

//Going from simple to large ..
 * func a(p:String){
 * print("hi \(p)")
 * }
 * a(p: "Db")


 * func a(r p:String){
 * print("hi \(p)")
 * }
 * a(r: "Db")


 * func a(r p:String, w u:String){
 * print("hi \(p) be \(u)")
 * }
 * a(r: "Db", w: "ok")


 * func a(r p:String, w u:String) -> String {
 * let t = "hi \(p) be \(u)"
 * return t
 * }
 * print (a(r: "Db", w: "ok"))


 * func a(p:String) -> String {
 * let t = "hi \(p)"
 * return t
 * }
 * func ab(p:String) -> String {
 * return "bb \(p)"
 * }
 * func a(p:String, c:Bool) -> String {
 * if c {
 * return ab(p:p)
 * } else {
 * return a(p:p)
 * }
 * }
 * print(a(p: "Db", c:false))
 * print(a(p: "Db", c:true))


 * func a(p:Int, c:Int = 12) { //defualt value is 12 so if not entred it will be 12 and be inserterd auto
 * print("\(p) \(c)")
 * }
 * a(p:3, c:6)
 * a(p:4)


 * // Note: It's good to have verb at first of naming that describe what the function will do

Lesson 5
//Classes
 * //Swift is Object-oriented programming language (OOP) لغة برمجة كائنية التوجه
 * //Objects vs linear اي تشغل الكائنات وليس فقط سطر وراء سطر
 * //class is like a template
 * //object is like a form of that template
 * //example of creating a class
 * // var a = 5 // this is creating a variable // class a { ... } // this creating classes
 * class a {var b = ""; var c = ""; var d = 0.0; var e = ""}
 * //example of creating object by that class. (will somehow makes variables of that class that can be used)
 * var h:a = a //وكانه نوع جديد من المتغيرات var vNum:NewType which is a class
 * h.b = "123456789"
 * h.c = "10.12.2012"
 * h.d = 0.0
 * h.e = "hehe"
 * // var h:a = a لسان حال المبرمج يقول: عرفلي اتش ويكون من نوع أي فاقدر اصدر منه كافة مستقبلات أي
 * // var h.b = "" etc يعني لما عملت اتش:اي وكاني عملت
 * // var ObjectName:className = className // creating object of class, will create by that:
 * // ObjectName.Variabel'sclassName = ""
 * var k:a = a
 * k.b = "5678956789"
 * k.c = "5.5.1979"
 * k.d = 70
 * k.e = "Hoho"
 * //print(a) //Error, you can't print class content all at once using pure print command
 * //print(a) // i don't know what's this
 * //print(a.self) // i don't know what's this
 * //print(h) //Error
 * //print(h) //Error
 * print(k.e)
 * //inheritance توريث
 * class g:a {var f = ""} // and now all a is within g like if it is really g have all a, but not vice versa. لسان حال المبرمج يقول: عرفلي جي ويكون فيه أي وإف
 * //k.f = "hihi" //Error, f variable isn't in class a, it's in the larger class g
 * var t:g = g
 * t.e = "haha"
 * t.f = "hihi"
 * print(t.e)
 * print(t.f)
 * print(k.e)
 * //inheritance توريث
 * class g:a {var f = ""} // and now all a is within g like if it is really g have all a, but not vice versa. لسان حال المبرمج يقول: عرفلي جي ويكون فيه أي وإف
 * //k.f = "hihi" //Error, f variable isn't in class a, it's in the larger class g
 * var t:g = g
 * t.e = "haha"
 * t.f = "hihi"
 * print(t.e)
 * print(t.f)
 * t.e = "haha"
 * t.f = "hihi"
 * print(t.e)
 * print(t.f)
 * print(t.f)

Exercising Classes & Objects
copy those examples to your playground and understand it:

Normal Example

 * class a {var b = ""; var c = ""; var d = 0.0; var e = ""}
 * var h:a = a
 * h.b = "123456789"
 * h.c = "10.12.2012"
 * h.d = 0.0
 * h.e = "hehe"
 * h.c = "10.12.2012"
 * h.d = 0.0
 * h.e = "hehe"


 * class useraccount {var createdDate = ""; var userID = ""; var palance = 0.0; var lastName = ""}
 * var Khaled:useraccount = useraccount // بهذه الطريقة منكون عرفنا كائن جديد يستمد من الكلاس مستقبلات وقيم ابتدائية
 * Khaled.createdDate = "123456789"
 * Khaled.userID = "11.11.2010"
 * Khaled.palance = 4.0
 * Khaled.lastName = "hoho"
 * print(Khaled.createdDate)
 * Khaled.palance = 4.0
 * Khaled.lastName = "hoho"
 * print(Khaled.createdDate)
 * print(Khaled.createdDate)

Simplest Example

 * class useraccount {var createdDate = ""}
 * var Khaled:useraccount = useraccount
 * Khaled.createdDate = "123456789"
 * print(Khaled.createdDate)
 * Khaled.createdDate = "123456789"
 * print(Khaled.createdDate)
 * print(Khaled.createdDate)


 * class a {var b = ""}
 * var h:a = a
 * h.b = "123456789"
 * print(h.b)
 * h.b = "123456789"
 * print(h.b)
 * print(h.b)

Simple Example

 * class useraccount {var createdDate = "", userID = ""}
 * var Khaled:useraccount = useraccount, Hamed:useraccount = useraccount
 * Khaled.createdDate = "123456789"; Hamed.userID = "10.10.2020"
 * print(Khaled.createdDate)
 * print(Hamed.userID)
 * class a {var b = "", c=""}
 * var h:a = a, j:a = a
 * h.b = "123456789"
 * j.b = "123456789"
 * print(h.b)
 * print(j.b)
 * var h:a = a, j:a = a
 * h.b = "123456789"
 * j.b = "123456789"
 * print(h.b)
 * print(j.b)
 * print(j.b)


 * //more equivalent
 * class a {var b = "", c=""}; var h:a = a, j:a = a; h.b = "123456789"
 * j.b = "123456789"
 * print(h.b); print(j.b)

More Equivalents

 * class useraccount {var createdDate = "", userID = "", palance = 0.0, lastName = ""}
 * var Khaled:useraccount = useraccount, Hamed:useraccount = useraccount
 * (Khaled.createdDate, Khaled.userID, Khaled.palance, Khaled.lastName) = ("123456789", "11.11.2010", 4.0, "hoho")
 * Hamed.createdDate = "123456789"; Hamed.userID = "10.10.2020"
 * print(Khaled.createdDate)
 * print(Hamed.userID)
 * class a {var b = "", c=""}
 * var h:a = a, j:a = a
 * (h.b, h.c) = ("123456789", "55")
 * j.b = "123456789"
 * print(h.b)
 * print(h.c)
 * var h:a = a, j:a = a
 * (h.b, h.c) = ("123456789", "55")
 * j.b = "123456789"
 * print(h.b)
 * print(h.c)
 * print(h.c)


 * // Note: you can put functions in classes