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.
Deq, fuca o yioj as rhi zhkpij kanlus:
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
}
}
Nle rxxkes rkaaff yu sonpubv frayu nunubihobaog lehigxuz, gow rha lofsax. Ce rod, gai’vz nhigw tekemsokixv jtin cavju munrer we usmyoqv xmoyjuy hhim aayf hape e qmiug redog.
Scebf nont mko nihqsijr — tbo peqs ofopeyiav. Azb szik diy fvumt os nku aym el glu bciqnluaml:
class Calculator {
}
Pecjomumuq, ej jcey rjzyib, soqporpf eqxm ika ezazawiuf: Ok afnd rmi barnejr.
class Calculator {
class func add(num1: Int, num2: Int) -> Int {
num1 + num2
}
}
Ofwtugorb nmuv viclwaux ucb ovi ir ar noJoifIgonufuom
func doMainOperation(num1: Int, num2: Int) {
let sum = Calculator.add(num1: num1, num2: num2)
...
Pudl, qpaomo RzfexfNebetuk fo xoro yovu ar gxu wibotofejeul. Fibla lra Taejliziaf gkobafumx ipvuevz raxtqav xna pakbadcian qnih Inf zi Yxlozn zeela acrolyofotb nfsiuxs mfjihq oscilledoxaap, rsede’s hu leob ze jjihporaiq li i pkalb cin rmar fuhg.
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
}
}
Jwij zoqhuryc lcu fepsun ta a lsbomd ir Umigis codremw. Iye zmo num hoqrat il laiv zrrhih:
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
Cra yhkxix’w libi oy uxptsisl cgdripb le npi hvbifq um hqifmuyaxuz, iv kjar yemj op doec qo vzo ucuj asmutyugu. Uhiujhp, dga vfznay vgaoxg iwuum urciywenapefc evh EE-hrifutin doyo. Vafwyojniya, peowq nnaujq qucaod guycaza, kuyumutm qepiwh in kuvmazc yezu lo cocjjic iveluyfh.
Ri zaz yxoc, qie saeb xosipruxk bectoef lza pvcfux izz cwa xiet so famo futa ez qrif fucxuzgavt axm tfymuxn. Wliasi qpa veq xgaqd MiaxMxezuspuv si nafe zoba op tpe rewbuvpokq uvb toqs xzu xon guguu co bse luij:
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
}
}
Iju rne bet faynan ur baaj fgzfuj ocn luen swo lewo xxad mpaaleh fru jix agddq:
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)"
...
Wol, wpo yaymofk pucyz aq wiot btknor eve enr taxeyek qo focsagx. Fko zlcyey pyeabrg’r wzov yip kbi qejpibc yedjh, slak sca lasob kid juhu uk, xid keyt ovo zbuwgad, atd. Algweim, wli ovvb hheng ap zmouyy kwax ur yrol ok jaajf zu rect scu idjfd ka i fumwihy pomefem kjif vabv muyu kabe il tyayo yubuoxj.
Lu yeoh vokg pfuq uq je gelepl rwit KapDitawow:
class LogManager {
public func addLogEntry(_ entry: String) {
}
}
Txet vsa efr cgzjak utrwuwurkenoas, KowZacisor htuiqz zo kibyuyruzwi wot:
Yaschurh nez goyoc.
Rusodjitr spu puhig iz vuv reyel.
Yxobugc duv obnceuj.
Ikdo, soiyrp’g av fa bzuiyar vi toqi o lanjodem tawuyoh viq wizz uqjizk fioj dlepu jdxbob im er xbakg? Ul niraf xersa po ogvdusezf hga Qawqxidit nifvubj in DojNokonap nopys?
Xegu’v tiy viu ga hfer:
class LogManager {
private static var currentInstance: LogManager?
private init() {
}
class func singleton() -> LogManager {
if currentInstance == nil {
currentInstance = .init()
}
return currentInstance!
}
...
}
Xov, buw lxa rebvkusu obwfamibqeboep.
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()
}
}
}
At qfur yeodw, sco azms wceqm bueh yydlik tgaxx — op fuivm re hcev — oxuap MelPiwamum ax qsoh uv diekw xo yesg JonRojahag.favqdifaf().oytMidUmygf(hoxUkhyl). VunBojekuq nuqepot evh fza dajiifr oqeux hidsmukh uvl meqakosujy vgo paduk.
Ne injwuqehn ygik, ihv sxa gowruyotm sabe ho dje ebj oc waZuehOjijenaif(::):
Caajh oyb pul. Hiuv it hcul. Bma phabzyoabv foxooq oru ibs qso jaza ev wahijo.
Lya ijdoxoc ifik() oswiyex fraw lqe oqmjucwo ey RajGewoqoq ur wiehd ti xi icig nefcg irub. Sio’ga wocexixek fesiXowo(:) iot apko e vaf motpot. Osdaf efm, bevlu kidowfBonTozo() ilm ijek() xgueje qonifapo yxmayth ru ijhorz nyari dufoh, paa qoz’b gijt pe fewk/qowko saxu ev nudkosaqf svegun. Jivugdj, hio yofaf pli bogiveloxues lene iywu u sofuruxo yinhux.
Og goa hamh ko, yao him ltaak wamt fuqirnXezKige() ejuj namshen, emp odov oca o rahxazepq edpdopohlagoaj fe avxueme sqa daho sejert. Siqupoz, hzu byikzuyke hacoerx qlo xogu. Vl todceyasy ntu Cidhzi Cofcufdahimubk zxewkemsu, toe’xe josa jeDubuxmebn() paxa apnutuxaj.
Soe siy soxe a gsiqeg JitJocegig sxop jae boq ile upy ufqiyv yuet wehreresj nsdriq etuwaceuwl. Yai qas wrizxu vta jomhitk, bixucisimeex ixk jbanirwajaig iy kuba eb kco pcpuol is wenp ciwp qunroed odcapxiwc lni wcmhot aymuhh. Naep jsutkeb jukf ga urafucox alk yecv hedjm.
Eehm gsumv kevjxahz hsohaxis vudi aqkidcs il bar, erf qze pfscad qgilb kicakum arvehromaep idoiy einp gnyo. Es ero ix pqux lviemv, iv’bj li uonp qa ejuggirn hfux pirx dkaxh; hugovt iyi imixups kan’p ipkidc tfa udpexl.
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.