Skip to content

Instantly share code, notes, and snippets.

View petermolnar-dev's full-sized avatar

Peter Molnar petermolnar-dev

View GitHub Profile
@petermolnar-dev
petermolnar-dev / CountryDetector.swift
Created June 28, 2022 20:10
Detect the country without geolocation, from the Contacts framework
import Contacts
class CountryDetector {
func detectCountryWithoutLocation() -> String {
CNContactsUserDefaults.shared().countryCode
}
}
@petermolnar-dev
petermolnar-dev / CountryDetector.swift
Created May 6, 2022 14:57
CountryDetector_With_Locale_fallback
import CoreTelephony
class CountryDetector {
var telephonyInfo = CTTelephonyNetworkInfo()
func detectCountryWithoutLocation() -> String? {
guard let provider = telephonyInfo.serviceSubscriberCellularProviders?.first(where: { $0.value.isoCountryCode != nil })?.value else {
return Locale.current.regionCode
}
@petermolnar-dev
petermolnar-dev / CountryDetector.swift
Created May 6, 2022 14:55
CountryDetector_Original
import CoreTelephony
class CountryDetector {
var telephonyInfo = CTTelephonyNetworkInfo()
func detectCountryWithoutLocation() -> String? {
guard let provider = telephonyInfo.serviceSubscriberCellularProviders?.first(where: { $0.value.isoCountryCode != nil })?.value else {
return nil
}
alias ..="cd .."
alias gps="git push"
alias gpl="git pull"
alias gcom="git commit -m"
alias gcd="git checkout"
alias gs="git status"
alias gm="git merge"
alias gl="git log"
alias grem="gir remote -v"
alias gadd="git add "
typealias HKSamplesResult = Result<[HKSample], Error>
func fetchAllBloodPressure(completion: @escaping (HKSamplesResult) -> ()) {
let correlationType = HKCorrelationType.correlationType(forIdentifier: .bloodPressure)!
let query = HKCorrelationQuery(type: correlationType, predicate: nil, samplePredicates: nil) {
(query, results, error) in
guard let correlations = results, error == nil else {
print("Error during the query: \(String(describing: error?.localizedDescription))")
completion(.failure(HKStoreManagerError.QueryExecutionError))
return
@petermolnar-dev
petermolnar-dev / value-reference.swift
Created January 3, 2021 19:38
Value and reference type in Swift
import UIKit
var str = "Hello, playground"
var a = 1
var b = a
b = b + 1
/// Value of `a` doesn't change when the value of `b` changed. This is the default behaviour of Value types
print(a)
@petermolnar-dev
petermolnar-dev / Numberformatter+minus.swift
Created December 3, 2020 12:36
NumberFormatter minus
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.usesGroupingSeparator = true
formatter.locale = .current
formatter.minimumFractionDigits = 2
formatter.maximumIntegerDigits = 8
//formatter.negativeFormat = "¤ -#,##0.00"
//formatter.positiveFormat = "¤# ##0.00"
@petermolnar-dev
petermolnar-dev / NumberFormatter.swift
Created November 30, 2020 15:16
currencyCode, currencySymbol with NumberFormatter experience
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.usesGroupingSeparator = true
formatter.locale = .current
formatter.minimumFractionDigits = 2
formatter.maximumIntegerDigits = 8
let number = 100.2 as NSNumber
@petermolnar-dev
petermolnar-dev / BloodPressureClassifierDense.swift
Created March 24, 2020 20:32
Blood Pressure Classifier dense
class BloodPressureClassifier {
static func classifyBloodpessure(systolicValue: Int, diastoicValue: Int) -> BloodPressureClassification {
switch (systolicValue, diastoicValue) {
case (SystolicRanges.severeHyperTensionRange, _), (_, DiastolicRanges.severeHyperTensionRange):
return .severeHypertension
case (SystolicRanges.moderateHypertensionRange, _), (_, DiastolicRanges.moderateHypertensionRange):
return .moderateHypertension
case (SystolicRanges.mildHypertensionRange, _), (_, DiastolicRanges.mildHypertensionRange):
@petermolnar-dev
petermolnar-dev / BloodPressureClassifier.swift
Created March 22, 2020 21:55
Blood pressure classifier
class BloodPressureClassifier {
static func classifyBloodpessure(systolicValue: Int, diastoicValue: Int) -> BloodPressureClassification {
switch (systolicValue, diastoicValue) {
case (SystolicRanges.severeHyperTensionRange, _):
fallthrough
case (_, DiastolicRanges.severeHyperTensionRange):
return .severeHypertension
case (SystolicRanges.moderateHypertensionRange, _):