Variable was written to, but never read
Big Nerd Ranch » Swift Programming
by hairyyak
1M ago
Heres’ the code: Blockquote import Cocoa enum Token : CustomStringConvertible { case number(Int) case plus case minus case multiplication case division var description: String { switch self { case .number(let n): return "Number: \(n)" case .plus: return "Symbol: +" case .minus: return "Symbol: -" case .multiplication: return "Symbol: *" case .division: return "Symbol: /" }; } } class Lexer { func getNumber() → Int { var value = 0 while let nextCharacter = peek() { switch nextCharacter { case "0"..."9 ..read more
Visit website
Chapter 1, Bronze Challenge
Big Nerd Ranch » Swift Programming
by geraldinejns
2M ago
Hello, I’m a newbian. Below is my first challenge in swift programming book. Just want to know if I read the challenge right. var lastName = “Jones” print(lastName) 1 post - 1 participant Read full topic ..read more
Visit website
Chapter 19, Electrum Challenge Solution
Big Nerd Ranch » Swift Programming
by hooker
4M ago
I made the ProgrammingLanguage enum conform to the CaseIterable protocol, giving me access to the type’s allCases property. I iterator on the property’s collection of values and print their rawValue property. For the gold variation of this challenge, I use the shorthand syntax $0 to access the closure’s one argument. The map(_:) instance method returns an array of the given argument’s type, and I pass the array as an argument to the print() method. // Electrum challenge enum ProgrammingLanguage: String, CaseIterable { case swift case objectiveC = "objective-c" case c case cpp ..read more
Visit website
Chapter 19, Bronze Challenge Solution
Big Nerd Ranch » Swift Programming
by hooker
4M ago
I moved the columnWidths[j] - item.count calculation into a constant, widthDelta. I then used the ternary operator to evaluate whether widthDelta has a negative value. If the column label’s width is less than the item’s width, then no padding is needed, and I assign 0 to paddingNeeded. func printTable(_ dataSource: TabularDataSource & CustomStringConvertible) { print("Table: \(dataSource)") // Create a header row containing column headers var headerRow = "|" // Also keep track of the width of each column var columnWidths = [Int]() for i in ..read more
Visit website
Chapter 17, Gold Challenge Solution
Big Nerd Ranch » Swift Programming
by hooker
4M ago
I added a question mark to the init keyword in the Monster class to specify a failable initializer. I also added a guard statement to prevent monsters from having an empty string for a name. // // Monster.swift // MonsterTown // import Foundation class Monster { static let isTerrifying = true class var spookyNoise: String { return "Grrr" } var town: Town? var name: String var victimPool: Int { get { return town?.population ?? 0 } set(newVictimPool) { town?.population = newVictimPool } } re ..read more
Visit website
Chapter 17, Silver Challenge Solution
Big Nerd Ranch » Swift Programming
by hooker
4M ago
I modified the required initializer of the Zombie class to call across the class to the class’s only designated initializer. I moved the default values for walksWithLimp and isFallingApart into the designated initializer’s parameters for limp and fallingApart. I also added the convenience keyword to the required initializer. // // Zombie.swift // MonsterTown // import Foundation class Zombie: Monster { class override var spookyNoise: String { return "Brains..." } var walksWithLimp: Bool private(set) var isFallingApart: Bool init(limp: Bool, fallin ..read more
Visit website
Chapter 16, Gold Challenge Solution
Big Nerd Ranch » Swift Programming
by hooker
5M ago
Below is the complete project. It does not run because I wanted to verify the mayor’s anxiety level is private. Comment out the mayorsAnxiety declaration and final print() statement in main.swift, or remove the private access level from the mayor’s stored instance property to get the code to run. // // main.swift // MonsterTown // import Foundation var myTown = Town() let myTownSize = myTown.townSize print(myTownSize) myTown.changePopulation(by: 1_000_000) print("Size: \(myTown.townSize); population: \(myTown.population)") let fredTheZombie = Zombie() fredTheZombie.town = myTown fredTheZ ..read more
Visit website
Exchange Rate API JSON
Big Nerd Ranch » Swift Programming
by claraclark
8M ago
In the contemporary financial environment, an Exchange Rate API in JSON format is essential since it provides a systematic and understandable approach to retrieve crucial currency exchange rate data. As a compact and legible format, JSON (JavaScript Object Notation) is especially well-liked for data sharing in the context of foreign exchange rates. This API makes historical or real-time data on currency exchange rates available to users, making it a vital resource for businesses, financial institutions, and developers. Users may effectively monitor and evaluate currency changes thanks to the d ..read more
Visit website
Gold Challenge Solution Chapter 23
Big Nerd Ranch » Swift Programming
by Swiftness
9M ago
I got pretty stuck on this for a while, figured someone else might appreciate the assist. The following is how I implemented it. You might need to change around some other details but the core of it is below. Not sure how optimal it is but it seems to work. func getNextExpression(_ value: Int) throws -> Int { guard let token = getNextToken() else { return value } let value2 = try getNextNumber() switch token { case .plus, .minus, .multiply: let value3: Int switch token { case .plus: valu ..read more
Visit website
Chapter 16, Bronze Challenge Solution
Big Nerd Ranch » Swift Programming
by hooker
9M ago
I modified Town.swift to satisfy the challenge requirement. // // Town.swift // MonsterTown // import Foundation struct Town { static let world = "Earth" let region = "Middle" var population = 5_422 { didSet(oldPopulation) { if population <= oldPopulation { print("The population has changed to \(population) from \(oldPopulation).") } } } var numberOfStoplights = 4 enum Size { case small case medium case large } var townSize: Size { switch population ..read more
Visit website

Follow Big Nerd Ranch » Swift Programming on FeedSpot

Continue with Google
Continue with Apple
OR