Big Nerd Ranch » Swift Programming
26 FOLLOWERS
A forum for programming guides written by Big Nerd Ranch. This section of the forum is for programmers to ask questions and get answers for swift programming language.
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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
Big Nerd Ranch » Swift Programming
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