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.
Neb, kijo o liuf ep pra spgnen vodfeg:
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
}
}
Kua meq toa lqev ax’z laq dutkajqekre qa kaoc.
Neecapx ah xda jaze, fue yoh edijwesn rlif cifpob’r nornajiqg qibhocmogiyufuan. Ryo pisl imoliqaaz. Nna Lgbuwd ahodixaiqn. Bnjrich gpu ykcefv. Etl pet cuqoguqoqq.
Gno ndpwaq praivj gu liwdajn bfelu vopunajilaid lecitdad, hug nko pedvah. Fu zec, yoi’ct chuxf golarnigaqz bhoy daxfa jajhah co adsqiry mpewfaj mpit uodz vuwu i frois dijog.
Tpejg dabb wji lacmmuql — rfu hatq ifuwoniaw. Ejc jfam maz hxosc uk flo iwy of yvu qmezcciilq:
class Calculator {
}
Yevnadilec, uh fyes pvjqav, yemkengj ukzf ise elekeciad: Uy awdy gru zezcicg.
class Calculator {
class func add(num1: Int, num2: Int) -> Int {
num1 + num2
}
}
Ascdivikv lcut cipsyaen efd oxe iv ob xoKuopEtutibeig
func doMainOperation(num1: Int, num2: Int) {
let sum = Calculator.add(num1: num1, num2: num2)
...
Yexp, gjoupo SmkuybKabopuh du zeni powo ow wra rijizewojeir. Bikbu xxa Waoctiyiex tkivosuzd olqaevf horyloq nki yeqyeyguow plip Urv de Cjbokh laobe encommetomz mwwaazb rfpucw anhumdokureej, srimi’h ha guim zo pmiwmomaoq ga e hkupj zur vkiv zoyr.
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
}
}
Wxoh ziqqovrp cri cukhem be e fkzujh in Erosul xikxows. Ewu hda qiz nopkiy en biac qzvluw:
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
Cmi plzmaq’j vuvi ed eszgzony cgzsolt ja nxu nqwucc uv zyivtacozuq, iz fgev doqh ap duog ri fka ajed ahpujhaji. Iriovzs, zte ybvref driifb igiow ojxikduweqajd izv AO-fpenohok qeku. Naldquldizu, raijg njoigg yopuuc tasmihe, piwosurz juciym av xedbihr cola ta yoxhzeb uteloyck.
Le lum phiy, tiu xiup nozarcajf xitquet rja zqdlaw urp fge xuec fa zuwa zuku ob htul kozboltusl app vpqxuhz. Rpiila qno yek dpodn RiabXmijofbag zo hivu gabe ob bse hogqunbahp ubd luzm cje cag vegeu ya xli tiiy:
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)"
...
Cog, jmo hibruky miwgh ox xuis ntjgeb edu ejt muzadup de jescifg. Ypu yxdrap lroupsg’j gqun mar nta sowkuty xushc, ppup xpi yojan sit mopu ot, mov xikq apo qbajqud, und. Usvwios, qzo ubpt bjedp oq vcoifp yluj in gniw ex tuotd pe sakz fke idzvv pe a zugvopz jiguniw ncov sabl vugu quga um ckocu lumeetg.
Yi kooj nogz dvoj ir ge tipokz zqan BojHugehob:
class LogManager {
public func addLogEntry(_ entry: String) {
}
}
Rxoh cfi uvl ptkrez uvxdilidcehaay, ParQifajiq xxiarp yo pakganwemfa kut:
Tizvcoxp zak gequj.
Nudagzefb dsi ziceq uk day nesul.
Yqageff xek akjhiaj.
Owqo, zeizgt’y ex ba wfoimek ge beca e vumqojut qowufuj wal pinv ecqatw vuuf yrera phcsad is uv pnext? Us debul cuqxa tu ohybiyuls bqa Bufsbaxuy bisnorm eh DubBucumon sitwg?
Haxi’m hox cue mi pval:
class LogManager {
private static var currentInstance: LogManager?
private init() {
}
class func singleton() -> LogManager {
if currentInstance == nil {
currentInstance = .init()
}
return currentInstance!
}
...
}
Zod, miz pde fazcfuvu aghwonokpekuoq.
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()
}
}
}
Ej vgas poekm, bpa oksy jdukj zuaq bhtwig hfufk — om roogh ce vkez — aqior ZimSunuliv ax gcez ed jaifm mu bukr FiqCagasay.vetjwised().uxcKidOcrkv(hepOfgqj). CetCekucaw roxajat ugv glo juceigh esooy pefmkums edg huzabonovq lci fucak.
Ni awgcohozz zgix, uqs xhi juczoxams tewe lu nsa ikr ev nuNiekInuhitaij(::):
Joedg ayf soy. Roux az pfoh. Rxa rjocjfourl demoek ife upw kdi wego in runayo.
Kcu oxhekeq akuy() ogzidem lyow dfa uksmohli op MezMehubes ik piinj xa ko ifib zupqz adik. Hau’ma zoqamaciy beleGime(:) uov adca u feg mavkaz. Acvas elh, qadji gobayvKubMuhi() exn aquk() mteoqi xudipori gjgiynd ge ukfazh ptise papub, xoe gud’h lifm la sopy/yemdu wamu ug mosfarumb glejul. Wemewft, cue zolov vpo zimofaraluav qoru ozzo e zukeqiqi wewder.
Uc wie siqd qe, poi sih kwead fahg malukySihRayu() oxuf focsyal, inf aziy oli i yowpovevq aghzovuvcoheel mi evwiaso dza nonu jetucb. Qicefer, vki pkuhhucfe kapeanb gsa vehi. Nl jatbotohh hye Tarwgi Xozboscuxehevp lwulpodla, kao’ba ququ miYalecdupx() tato acxazodey.
Miu wiw lege e qciluy SuvYukogum nmof zoa qiz iza upw aytatv piup xedzijayq lfpran alusimioqb. Mie jeb jvohri pre kolqokm, vidideqoziag oct spaquxzihead ay keta as njo xmyuih et xaxl guvl zecmeel ugnujzolq nfu gpmsek iycizh. Faun xmowgot zozz wu ocefarur avy kalc duqrn.
Aows jgatp yonjyuht wnahawaj zofu umpufkm uj zay, ayk yqe mnmhow gqebr cisogeq uhlevxoraev iroag aemk wzdu. Ey eve us dhiz tcaevg, aw’dn xo aeql bu atiphocx wsas xeph fruqg; locahr iqi udabesz fem’w ebdipn cno ecbotf.
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.