Skip to content

Instantly share code, notes, and snippets.

View RinniSwift's full-sized avatar
🔸
Swifting

Rinni Swift RinniSwift

🔸
Swifting
View GitHub Profile
// 1.
class CircularBuffer<T> {
var bufferMaxSize: Int
var size = 0
// 2.
var elements = Array<T?>(repeating: nil, count: 8)
// 3.
init(maxSize: Int) {
let array = ["Hello", "people", "of", "the", "world"]
// looping using count property
for ind in 0..<array.count {
print(ind)
}
// 0, 1, 2, 3, 4
// looping using the endIndex property
for ind in 0..<array.endIndex {
// 1. empty array initialization
let numbers = [Int]()
let nums: [Int] = []
let newNumbers = Array<Int>()
let newNums: Array<Int> = Array()
// 2. preinitializing a fixed number of default values
let repeatSwift = Array<String>(repeating: "Swift", count: 5) // ["Swift", "Swift", "Swift", "Swift", "Swift"]
// or
let repeatSwift = Array(repeating: "Swift", count: 5) // ["Swift", "Swift", "Swift", "Swift", "Swift"]
ServiceLayer.request(router: Router.getProductInfo) { (result: Result<[String : [CollectionItem]], Error>) in
switch result {
case .success:
print(result)
case .failure:
print(result)
}
}
struct CollectionInfo: Codable {
let title: String
let productType: String
let variants: [VariantsInfo]
let image: ImageInfo
enum CodingKeys: String, CodingKey {
case title
case productType = "product_type"
case variants
struct CollectionItemId: Codable {
let productId: Int
enum CodingKeys: String, CodingKey {
case productId = "product_id"
}
}
struct CollectionItem: Codable {
let title: String
let id: Int
}

What will I refactor?

old code base line 40 to 110

  • The action sign in button.
    • to reduce amount of lines in code.
    • to reduce redundent code.

How will I refactor it?

  • create helper functions for independant tasks, and to reduce redundent code.
  • refactor the networking layer to push tasks into the background thread instead of pushing it to the background thread everytime I call the object.\
class ServiceLayer {
// 1.
class func request<T: Codable>(router: Router, completion: @escaping (Result<[String: [T]], Error>) -> ()) {
// 2.
var components = URLComponents()
components.scheme = router.scheme
components.host = router.host
components.path = router.path
components.queryItems = router.parameters
// 3.
enum Router {
// ...
// 7.
var method: String {
switch self {
case .getSources, .getProductIds, .getProductInfo:
return "GET"
}
}