Skip to content

Instantly share code, notes, and snippets.

View leoniralves's full-sized avatar
:octocat:

Leonir Deolindo leoniralves

:octocat:
View GitHub Profile
@leoniralves
leoniralves / markdown_examples.md
Created February 26, 2024 14:46 — forked from cseeman/markdown_examples.md
Markdown for info panel/warning box

Examples for how to create your own info panel, warning box and other decent looking notification in GitHub markdown.

All the boxes are single/two cell tables or two row tables.

Warning box

❗ You have to read about this
@leoniralves
leoniralves / Data+PrettyPrint.swift
Created November 14, 2022 19:31 — forked from cprovatas/Data+PrettyPrint.swift
Pretty print JSON string from Data in Swift 4.1 (especially useful printing to Xcode console)
import Foundation
extension Data {
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
@leoniralves
leoniralves / DiskStatus.swift
Created October 17, 2022 13:06
iOS Disk Status (Storage)
class DiskStatus {
// MARK: Get String Value
class var totalDiskSpace: String {
get {
return ByteCountFormatter.string(fromByteCount: totalDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.file)
}
}
class var freeDiskSpace: String {
@leoniralves
leoniralves / MemoryUsage.swift
Created October 14, 2022 20:34 — forked from pejalo/MemoryUsage.swift
Swift 4 iOS app memory usage
/// If an error occurs while getting the amount of memory used, the first returned value in the tuple will be 0.
func getMemoryUsedAndDeviceTotalInMegabytes() -> (Float, Float) {
// https://stackoverflow.com/questions/5887248/ios-app-maximum-memory-budget/19692719#19692719
// https://stackoverflow.com/questions/27556807/swift-pointer-problems-with-mach-task-basic-info/27559770#27559770
var used_megabytes: Float = 0
let total_bytes = Float(ProcessInfo.processInfo.physicalMemory)
//
// ExtensionURLRequest.swift
//
// Created by Abhishek Maurya on 16/07/20.
// Copyright © 2020. All rights reserved.
//
import Foundation
extension URLRequest {
@leoniralves
leoniralves / SimulateGestureOnView.swift
Created April 19, 2021 20:33
Simulate Gesture on Views in Swift (draft)
class ViewController: UIViewController {
private(set) var tap: UITapGestureRecognizer
init(tap: UITapGestureRecognizer) {
self.tap = tap
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
@leoniralves
leoniralves / SwizzleWithCustomObjects.swift
Created April 19, 2021 11:58
Swizzle methods between two custom objects
class MyObject: NSObject {
@objc dynamic func getName(_ name: String) -> String {
return name
}
}
class FakeMyObject: NSObject {
@objc dynamic func getName(_ name: String) -> String {
return name + " concat"
}
@leoniralves
leoniralves / AsyncDispatcher.swift
Last active March 29, 2021 22:15
To inject, mock, spy and stub DispatchQueue (sync/async).
import Foundation
protocol AsyncDispatcher {
func async(
group: DispatchGroup?,
qos: DispatchQoS,
flags: DispatchWorkItemFlags,
execute work: @escaping @convention(block) () -> Void
)
func async(execute work: @escaping @convention(block) () -> Void)
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func writeOnFile(str: String) {
let filename = getDocumentsDirectory().appendingPathComponent("output.txt")
do {
if FileManager.default.fileExists(atPath: filename.path), let data = "\(str)\n".data(using: .utf8) {
import Foundation
/*
"Method swizzling is the process of changing the implementation of an existing selector.
It’s a technique made possible by the fact that method invocations in Objective-C can be
changed at runtime, by changing how selectors are mapped to underlying functions in a
class’s dispatch table."
Source: https://nshipster.com/method-swizzling/
"This is only possible because in Objective-C the method to call when a message is sent