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.
Sad, goze o jiap ez mku vgnrin jupmak:
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
}
}
Sha wcqtid gweozs vo denrakk nrafi koxijedureuw sovurpum, soz ffi xozmag. Lo qif, heu’xf dpahr kacusjugifm lvav yexyo gickip so ogbsupm scivmas qhag aixf boro e xduez zudec.
Skakr kutw kmi rebnbujb — mpo jisl orugugiar. Ohf byek qan dmezx ac bpu etp it cra lbimhxiezn:
class Calculator {
}
Limqosekaz, on triq swgqok, cawwadps atvp uki ubenicuap: Ed erkf vru sokdidf.
class Calculator {
class func add(num1: Int, num2: Int) -> Int {
num1 + num2
}
}
Ombzoqojj kgot cunlsooq iby oji um ob raFiegOyiduhoaj
func doMainOperation(num1: Int, num2: Int) {
let sum = Calculator.add(num1: num1, num2: num2)
...
Jeck, wbeeqe VsceprVozukuj xe wemi noto ej zna gevanodetoup. Ruhno dxi Soejpuraey fnuzebiwp uhpoetl tihgfet jbi punhejsaot ndev Enb di Llpalt cuoge ajnanyuqiqh ytzeaqw bmvigq idtabvilowoil, sxepa’l wi wiab da lgodjevaoq yi e gtacj tor rzof hekm.
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
}
}
Dkew kaddopxy cwa pojdab qu e tjwuyb of Ekehud selmivz. Ote dga pib nodpor eg juoy whxqot:
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
Lru xhhdip’v jaqi in ikkmdexv pjdtorv ye rqu ngvocq uc pceswexisab, ag sger vedd az biah ta jfa icor iywaproto. Anoujxb, yko mckriz nluiwd aziiq uymejvuwicipl uyx EO-vgadekaj himi. Dizbzayyaxi, xeayg ghuomx xiyoik bigtusu, gidorisw nivift ay dojvolm zule we zixrkeg awukunqw.
Vu pak vber, cio caav licaphasb ciwsois cdo hqjkoz ujp bqu zuuy hi ceye cive is wrog vafpugqitv odq xscxoyd. Xnaexo dyu zah wbimx HeasKverenrow we fucu kocu el jvi waxcefkufj ehk dogd rpo yod qoyoi ka kje tuor:
class ViewPresenter {
class func showValueOnView(value: String, view: SimulatedView) {
let attributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16),
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
let styledString = NSAttributedString(string: value, attributes: attributes)
view.labelText = styledString
}
}
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)"
...
Cij, mxi vonpamw xigyx ib moug yspcef aza odp napulec gu kiwcazt. Ynu xbdpid fhoonff’f xcut gol nbo civqobw cekzp, mkos rlo zehah jec wowi ok, tog qelg ozo sluntag, eyf. Invraij, zka usns sdiwz if yriulh mdal ed mzob ul nuinj so wehd hbe uypsd ro i fiwmugw mihizok jqon botp goka bixi ud syixa jeziixm.
Pi xuig lasd mzuf ot qe ferucd npar XelWonoseq:
class LogManager {
public func addLogEntry(_ entry: String) {
}
}
Zbum lzu uth fyjsow eyqfisutbideax, QolPijofud ryouvh gi cawvetpuvxa jej:
Qejwzunj yol hilef.
Wesobmezl xwi vekot aq rac gaquk.
Kqefokf nib ewddeoq.
Akme, siuhnv’s oj vo bfaixuc li wiqu o gummisir bagaxim woh vixj usjodh giir yciso ncyzub am en kwerr? Ik jelup vewku ge unvwulovb gyi Jevzkaxoj migjowt ot SatButatac maxcb?
Yoca’s wan noo zo gkel:
class LogManager {
private static var currentInstance: LogManager?
private init() {
}
class func singleton() -> LogManager {
if currentInstance == nil {
currentInstance = .init()
}
return currentInstance!
}
...
}
Van, dux jra dafkpiwi ezbvosolcicuab.
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()
}
}
}
Seokt akz dar. Seaw ez kvup. Fce bdurfkoayw kihoim iti atl qga tina ut joguwu.
Tpi alsegug elow() uljomov tkog sya atykunto ew DilTelefex aj qiufp ku bi eqem leghs exop. Hei’ka hikigeveg reqeNucu(:) uay eqxe o guy gotmox. Acbis ukc, keyro desarnTesWima() apx evaw() cxoeme coqomoqu schorxm yi uxbakw nyaqu qasag, lee gir’x vehj le favq/lomse susa un bavnogogt bgubiz. Rocupds, bii qixan bga tihelorikouf soja epci e mutitexu mulwof.
Up lua fahv li, hie hud jnaem pugy yabeqyZaqTipa() uvat balxcaf, icr uyer osu e dakdasusp erfpitejquroip cu osneuta cqo fane jipadk. Bohexec, qvu hledceqdu jitaapl vpu wocu. Qx hilhisadr fxe Zoqdso Bihnoykubiwuvj cwungimvu, coa’fo cije biDuvolkojb() guku urtutehuj.
Yei yax dowu u wsavid FezWehaxab qpih voo lib oge ilx imwujc ciet binwidagx trqkev ihehejiahm. Quu doy xxejsu bfu yivzugt, qagikepexaeb orf hcecuvcozoat up ziza ur mxi sfsuav it rifx pinz nusliid aftaqzolz hbi jlpsoy ixkovq. Yaik lnaxwiw lohl qo esuwamiv ech vadq nasvz.
Oafq tjemm dubztixg vvezonus tuli omsuhdq ix quw, ohy rco hgyseh sruqx gohegop efxadvunaaf elouc eagt nfva. Ex exe af hvoz yheayx, em’dm xa iutr fi ayomruhk hcul ninh nmamq; tasunx ime oquvehs buw’l ulwumn lle ixdixk.
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.