Skip to content

Instantly share code, notes, and snippets.

View novinfard's full-sized avatar

Soheil Novinfard novinfard

View GitHub Profile
@novinfard
novinfard / compactMapCombine.swift
Created March 12, 2021 09:59
[Different ways for replacing nil with Empty in Combine]
enum RemoteError: Error {
case networkError(Error)
case parseError(Error)
case emptyResponse
}
struct RemoteResponse: Codable {
let enitities: [Entity]
let numberOfEntities: Int
}
@novinfard
novinfard / network_with_cominbe.swift
Last active February 15, 2021 02:03
[Networking with Combine and Codable] #Combine #URLSession #networking
var subscriptions = Set<AnyCancellable>()
guard let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1") else { return }
// With Error handling
URLSession.shared
.dataTaskPublisher(for: url)
.tryMap { data, response in
try JSONDecoder().decode(SampleData.self, from: data)
}
@novinfard
novinfard / quizStateMachine.swift
Created September 4, 2020 12:08
[Quiz State Machine in Swift]
import Foundation
struct QuizQuestionItem: Equatable {
var allAnswers: [QuizAnswerItem]
var rightAnswer: QuizAnswerItem
}
struct QuizAnswerItem: Equatable {
}
@novinfard
novinfard / randomString.swift
Created April 27, 2020 10:09
[Random String]
extension String {
static func random(length: Int = 10) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
}
}
@novinfard
novinfard / URLRequest+curlCommand.swift
Last active April 22, 2020 15:28 — forked from dedeexe/URLRequest+curlCommand.swift
Convert URL Request to curl command
extension URLRequest {
public func curl(pretty:Bool = false) -> String {
var data : String = ""
let complement = pretty ? "\\\n" : ""
let method = "-X \(self.httpMethod ?? "GET") \(complement)"
let url = "\"" + (self.url?.absoluteString ?? "") + "\""
var header = ""
@novinfard
novinfard / debugJSONBeforeDecoding.swift
Created April 8, 2020 10:29
[Debug json before decoding and parsing]
#if DEBUG
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
print(jsonResult)
}
// OR
print(String(data: data, encoding:.utf8) ?? “Not UTF8”)
#endif
@novinfard
novinfard / StandingUnitTests-MCT.swift
Last active March 16, 2020 16:55
[Standing Unit Tests - MCT Article]
class StandingsEndpointLocalRequest: StandingsEndpointRequest {
let tournamentId: Int32
let file: String
init(file: String, tournamentId: Int32) {
self.file = file
self.tournamentId = tournamentId
}
@novinfard
novinfard / tournamentStandingsSample-MCT.json
Created March 16, 2020 14:35
[Sample JSON - MCT Article]
[
{
"tournamentId": 10,
"title": "A Standing",
"content": "A Content",
"groupName": "Group A"
},
{
"tournamentId": 10,
"title": "A Standing",
@novinfard
novinfard / StandingsDatasource_v2-MCT.swift
Created March 16, 2020 14:05
[Standings Datasource: After Refactoring - MCT Article ]
class StandingsDatasource: ManagedDataSource {
private let endpointRequest: StandingsEndpointRequest
private let entityRequest: StandingsEntityRequest
private let dataController: RequestDataConnectionController
init(endpointRequest: StandingsEndpointRequest,
entityRequest: StandingsEntityRequest,
managedObjectContext: NSManagedObjectContext,
dataController: RequestDataConnectionController) {
@novinfard
novinfard / StandingsEntityRequest-MCT.swift
Last active March 16, 2020 12:50
[Standings Entity Request - MCT Article]
struct StandingsEntityRequest: EntityRequest {
let entityName: String
let predicate: NSPredicate?
init(tournamentId: Int32,
groupName: String? = nil) {
self.entityName = "Standing"
let predicates = [