-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
152 lines (116 loc) · 4.37 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import UIKit
// structs: is a building block of a method in which we emulate an "object" in Swift
// struct syntax
// SomeStruct below defines an "object"
// Object-oriented programming (OOP) - encapsulates properties and methods of real world objects
struct SomeStruct {
// definition of the struct includes the following:
// properties - variables or constants on the type (struct)
// initializers - the method in which you create a type
// methods - function inside of a custom type (struct)
}
struct Resolution {
// properties
// stored properites - a property with a default value
// a property is a variable or constant
var width = 0
var height = 0
}
//===============================================
// create an instance of Resolution struct
//===============================================
var someResolution = Resolution()
// accessing properties of Resolution using dot syntax
someResolution.width = 1080
print(someResolution)
// structs have a ((memberwise initializer)) as opposed to
// a class that does not
var vga = Resolution(width: 640, height: 480)
print(vga)
//===============================================
// structs and enums are *****value types*****
//===============================================
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd // cinema gets a **copy** of hd
// ****COPY DOES NOT CHANGE THE ORIGINAL OBJECT****
// what is the value of cinema's here // 1920
print(cinema.width) // 1920
cinema.width = 2048
// what is the value of hd's width????
print(hd.width) // 1920
//====================================================
// using object-oriented programming to model a Fellow
//====================================================
enum ProgrammingLanguage {
case swift
case objectivec
case java
case javascript
case python
case noLanguage
}
struct Project {
var projectName = ""
var projectDescription = ""
var language = ProgrammingLanguage.noLanguage
}
enum Cohort {
case iOS
case android
case web
case dataScience
}
struct Fellow {
//==========================================
// properties - variable and constants
//==========================================
var name = "John Appleseed" // stored properties
var cohort = Cohort.iOS // iOS, android, web, dataScience
var programmingLanguages = [ProgrammingLanguage]()
var githubURL = ""
var linkedInURL = ""
var hasProgrammingBackground = false
var projects = [Project]() // projects
//==========================================
// initializers
//==========================================
//==========================================
// methods
//==========================================
// class methods
// instance methods
// can ONLY be called using instanceName.info()
// or e.g gregg.info()
func info() {
print("\(name) currently has \(projects.count) portfolio project(s)")
}
func resume(language: ProgrammingLanguage) {
// self represents the instance of Fellow that called
// resume() e.g gregg.resume(language: .swift)
if self.programmingLanguages.contains(language) {
print("\(name) can be hired for this \(language) position.")
} else {
print("\(name) is not qualified for this position.")
}
}
}
// creating instances of "objects"
// creating an instance using the default initializer of Fellow()
var gregg = Fellow()
gregg.name = "Gregg"
gregg.cohort = .iOS
gregg.projects.append(Project(projectName: "Hangman", projectDescription: "Hangman command-line macOS application", language: .swift))
gregg.info()
// Fellow.info() DOES NOT COMPILE NOT A CLASS METHOD
// created an instance of Fellow called oscar and used the memberwise initializer
var oscar = Fellow(name: "Oscar", cohort: .iOS, programmingLanguages: [.swift], githubURL: "https://github.com", linkedInURL: "https://linkedIn.com", hasProgrammingBackground: false, projects: [Project]())
oscar.info()
// intances of Project
let calculatorApp = Project(projectName: "Calculator", projectDescription: "Calculator command-line application", language: .swift)
let instagramClone = Project(projectName: "Instagram Clone", projectDescription: "Best photo sharing social app", language: .java)
// add projects to oscar's portfolio
oscar.projects.append(calculatorApp)
oscar.projects.append(instagramClone)
oscar.info() // 2 projects
oscar.resume(language: .javascript)
oscar.resume(language: .swift) // 🥳