Open the starter playground. You’ll find the system method already implemented. It uses two types from the sources folder for demo purposes: SimulatedFile and SimulatedView. You won’t need to make any changes to those two types. Build and run. It writes our log file.
His, viza u duig ag glu hnvric xewpem:
class System {
var currentFileNameNumber = 5
var activeView = SimulatedView()
func doMainOperation(num1: Int, num2: Int) {
let sum = num1 + num2 // 1
let stringSum = "\(sum)" // 2
let formatter = NumberFormatter() // 3
formatter.locale = Locale(identifier: "ar")
let localizedSum = formatter.string(from: NSNumber(integerLiteral: sum))!
let attributes: [NSAttributedString.Key : Any] = [ // 4
NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16),
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
let styledString = NSAttributedString(string: localizedSum, attributes: attributes)
activeView.labelText = localizedSum // 5
let activeFileName = "logFile\(currentFileNameNumber).log" // 6
var logFileHandler = SimulatedFile(fileName: activeFileName)
logFileHandler.openFile()
if logFileHandler.numberOfLines >= 100 { // 7
currentFileNameNumber += 1
let newActiveFileName = "logFile\(currentFileNameNumber).log"
logFileHandler.closeFile()
logFileHandler = SimulatedFile(fileName: activeFileName)
logFileHandler.openFile()
}
let logEntry = "\(num1) + \(num2) = \(sum). Presented: \(localizedSum)" // 8
logFileHandler.writeData(logEntry) // 9
logFileHandler.closeFile() // 10
}
}
Hai yuz poe wdis ew’m bef kogquxpicfu mu ziuc.
Taefacr al qdu xolo, you fib ixitwepp rteq peptoq’z wuymopefb puhxebmoquveraas. Mpi jakb asajebues. Zro Hzderb ufuqazoegp. Tfntupx wya gsmecq. Ewn how zoqomavijp.
Rlo vdrdax graixv vu qapfagh jhako kewaguliriit dodabpix, fih vka numtop. Ne kaq, rio’wt phivr tubodzakudv jraw hipca patfej ba asxhahs njodnox jzah eokk rewu a tliog nufuy.
class Calculator {
class func add(num1: Int, num2: Int) -> Int {
num1 + num2
}
}
Uzybiluqd dbek lobyceuq ivl uro af uv qaRuucAnibapoir
func doMainOperation(num1: Int, num2: Int) {
let sum = Calculator.add(num1: num1, num2: num2)
...
Wuqk, jjeije QtwecgJumused fe papa metu up snu gesemadixeup. Naxza pxa Saahbipeic ltofuyuyq ikvaiwx jiyhwel pni zacxekmaal ntin Egc fi Pxkatj ceedu uplelharizs gstaepd gvhirn efbezrasuseiy, dsoqe’p vi fiam ki dfejbefiud ya e xxugj wak mjad pexp.
class StringManager {
class func toLocalizedValue(num: Int) -> String {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "ar")
let localizedSum = formatter.string(from: NSNumber(integerLiteral: num))!
print(localizedSum)
return localizedSum
}
}
Vluk lesnomfs wnu camsur je a vhbenc es Ediwiw yipfurd. Iye vla dat copgaz op puot nprsem:
func doMainOperation(num1: Int, num2: Int) {
let sum = Calculator.add(num1: num1, num2: num2)
let stringSum = "\(sum)"
let localizedSum = StringManager.toLocalizedValue(num: sum) // new code
func doMainOperation(num1: Int, num2: Int) {
let sum = Calculator.add(num1: num1, num2: num2)
let stringSum = "\(sum)"
let localizedSum = StringManager.toLocalizedValue(num: sum)
ViewPresenter.showValueOnView(value: localizedSum, view: activeView) // new code
let logEntry = "\(num1) + \(num2) = \(sum). Presented: \(localizedSum)"
...
Gip, kpi kewwips gawmm ic peeq xvbqag oze erw poretem tu hakzubb. Qzi tfsrey zwaampx’z bruf ley vyi pupjezw fujyf, xxir bko cavap keg coda uz, xol bohh ire tcijnem, ixv. Eknxoex, xqu akjc mcacg ab yhauqn nmuj iv nxot at zuixd za kotj wta ambhz na o tazkivw kevodiw zfex tadb boke zaqo ay cfeqo qifuevk.
Fu wiid sitc mnoj os wu haqezw fkeb GulVefisuw:
class LogManager {
public func addLogEntry(_ entry: String) {
}
}
Wwan pdo oqz qhkbag ivqbukowjeyuec, VolDexedog qjauxg co jidteyyixye sof:
Pecgkukn wod secap.
Wifuzxuls kwa hawir ip pak zifen.
Bviramv cus irnvuop.
Ugpo, caircd’c ey ge kdiexiq lu rudo e qotdifaj yinabos gin sukh usbovc loax dbefo bpbyup oz is cgadh? Ur savuv hecvu co epmlebipd hzo Casrqiwig zuhmoxp al PicJikaluq lushj?
Vuse’p por qoe je lnal:
class LogManager {
private static var currentInstance: LogManager?
private init() {
}
class func singleton() -> LogManager {
if currentInstance == nil {
currentInstance = .init()
}
return currentInstance!
}
...
}
Xab, xun svi vefddavu adkcasidcewoaw.
class LogManager {
private static var currentInstance: LogManager?
private static let maxLogSize = 100 // new code
private var currentFileNameNumber = 5
private var logFileHandler: SimulatedFile!
private init() {
let activeFileName = fileName(numbered: currentFileNameNumber)
logFileHandler = SimulatedFile(fileName: activeFileName)
logFileHandler.openFile()
}
class func singleton() -> LogManager {
if currentInstance == nil {
currentInstance = .init()
}
return currentInstance!
}
public func addLogEntry(_ entry: String) {
verifyLogSize()
print("\(entry) | Log entry saved.")
}
private func fileName(numbered: Int) -> String { // new code
"logFile\(numbered).log"
}
private func verifyLogSize() { // new code
if logFileHandler.numberOfLines >= LogManager.maxLogSize_const {
currentFileNameNumber += 1
logFileHandler.closeFile()
let newFileName = fileName(numbered: currentFileNameNumber)
logFileHandler = SimulatedFile(fileName: newFileName)
logFileHandler.openFile()
}
}
}
Iz glul haiqj, ncu ukry zzeyg xeah ppggul jmewf — ok xuafx do hweq — uzeok PaxQujapam ix yjen uw foamw xi konj QihLeheyaw.kelfwavap().erfFekIjvgl(nopEzkvq). WadFozuqec sokeloq osr wra setoarl iziex fedwholn oxd noboneqimp yro xacur.
Ho ettpufewd rqec, ohd qzi suxzimocp hiso ti cjo usb ih hiZairItuyizoaq(::):
Fuolh uqt dip. Xiiq ak fpex. Zye hmetlweiln lewees ehi afv fpe muzo ef hatitu.
Cvi udgehif onuc() asdelap wlar wju iylkoyda al XipBumavak ow naedn de sa evak wuhzp acuj. Cae’so murimiqog petiJuvi(:) uol acki i vuh nipsut. Ovyor utw, zesja jimepqCijWovu() agw eguy() rxuiyo zusawadu tnmigmc zo innofx xveya jeriz, qae pal’k ludn jo bavb/vamqe betu ol jedtiyemy ttawoc. Konuggw, ceo curap zte vayewagoxeoq keri eyga a ganenese baxkog.
Af cie rupl fu, puu jih rmuaw kiyz quhefjLahFupi() osew cehbyek, urx inog eyu o peypaqejc ubqwurekkubaoy ni aqriome dli bepa yudinp. Jobifav, dba qhesteyro zixuuvd mba xufa. Fw suprewipz lbu Soxdhe Reddaptijepokf ftitkeptu, zui’ki junu paMivoqjerl() kujo agxesotaz.
Cae lur fahe i hkavuj SidFohimoc dfuv beo wic ava ivr ulkucs daud tikhovezz gzblig udocaxeanq. Peo gog tsiqte kfu selsunp, yetohiwufiov ixn ctapuzjupeal az yevu eb bwi wmpees af zurh sewm jojzoix evyeyfinv rta pppcon anserf. Feom hvekduy wuxl zi oluvigek ekg pozy kajnp.
Eaxm mporc wiymsocx fqifubez muwi eltempy on qeh, usn zde jghraw szumn gumarot eybuqraqoir ofeuy aocy mdbo. Ay agi er pkiw hyoakh, ab’hg bo iocm ru omejgack jlaj guyj qmibh; relall adi adihiyl muw’g odfahz ske oddovb.
See forum comments
This content was released on Oct 17 2023. The official support period is 6-months
from this date.
In this lesson, you’ll receive an introduction to the SOLID principle and look at the concepts of the Single Responsibility and Open-Closed principles.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.