This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OnDiskCache { | |
struct Item { | |
let path: String | |
let age: TimeInterval | |
let size: Int | |
} | |
var currentItems: Set<Item> { /* .returning current items.. */ } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol CleanUpPolicy { | |
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item> | |
} | |
struct MaxSizeCachePolicy: CleanUpPolicy { | |
let maxSize: Int | |
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item> { | |
var itemToRemove = Set<OnDiskCache.Item>() | |
let sortedItems = items.sorted { $0.age < $1.age } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol CleanUpPolicy { | |
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item> | |
} | |
struct MaxSizeCachePolicy: CleanUpPolicy { | |
let maxSize: Int | |
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item> { | |
var itemToRemove = Set<OnDiskCache.Item>() | |
let sortedItems = items.sorted { $0.age < $1.age } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User { | |
let balance: Double | |
} | |
func saveUserBalance(user: User, buyingAmount: Double) { | |
let remainingBalance = user.balance - buyingAmount | |
// save user balance for persistance | |
UserDefaults.standard.set(remainingBalance, forKey: "userBalance") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func saveUserBalance(user: User, buyingAmount: Double, defaults: UserDefaults = UserDefaults.standard) { | |
let remainingBalance = user.balance - buyingAmount | |
// save user balance for persistance | |
defaults.set(remainingBalance, forKey: "userBalance") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MockUserDefault: UserDefaults { | |
var balance = Double() | |
override func set(_ value: Double, forKey defaultName: String) { | |
balance = value | |
} | |
} | |
func testUserBalanceCalculation() { | |
let user = User(balance: 100) | |
let mockUserDefaults = MockUserDefault() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProfileViewController: UIViewController { | |
var user: User | |
convenience init(user: User) { | |
self.init(nibName:nil, bundle:nil) | |
self.user = user | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let vc = ProfileViewController() | |
vc.user = user | |
navigationViewController.push(vc, animated: true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DateFormatter { | |
static func convertToUTC(localDate: Date) -> Date { | |
// conversion logic | |
return utcDate | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias Amount = Float | |
struct User { | |
let name: String | |
let salary: Amount | |
} | |
enum PerformanceRating { | |
enum Result { | |
case warning(letter: String) | |
case bonus(amount: Amount) |
OlderNewer