Skip to content

Instantly share code, notes, and snippets.

View rogerluan's full-sized avatar
🚀

Roger Oba rogerluan

🚀
View GitHub Profile
@rogerluan
rogerluan / PHPickerViewControllerSample.swift
Created June 29, 2020 10:11
PHPickerViewController configuration
var configuration = PHPickerConfiguration()
configuration.filter = .images
configuration.selectionLimit = 0
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
@rogerluan
rogerluan / String+Regex.swift
Created July 27, 2020 02:26
Handy Regex utility to find and replace matching groups in Swift.
extension String {
/// Finds matching groups and replace them with a template using an intuitive API.
///
/// This example will go through an input string and replace all occurrences of "MyGreatBrand" with "**MyGreatBrand**".
///
/// let regex = try! NSRegularExpression(pattern: #"(MyGreatBrand)"#) // Matches all occurrences of MyGreatBrand
/// someMarkdownDocument.replaceGroups(matching: regex, with: #"**$1**"#) // Surround all matches with **, formatting as bold text in markdown.
/// print(someMarkdownDocument)
///
/// - Parameters:
@rogerluan
rogerluan / Regex+Utilities.swift
Last active July 27, 2020 02:51
A few regex utilities in Swift.
extension NSRegularExpression {
convenience init(_ pattern: String) {
do {
try self.init(pattern: pattern)
} catch {
preconditionFailure("Illegal regular expression with pattern: \(pattern)")
}
}
func matches(_ string: String, options: NSRegularExpression.MatchingOptions = []) -> Bool {
@rogerluan
rogerluan / Flattenable.swift
Created December 17, 2020 19:36
Flattenable conforming entities define flattened, a function that flattens any arbitrary Optional, no matter how deeply nested, down to a single level of optionality.
import XCTest
protocol Flattenable {
func flattened() -> Any?
}
extension Swift.Optional : Flattenable {
/// Flattens any arbitrary Optional, no matter how deeply nested, down to a single level of optionality.
/// - SeeAlso: Really fun discussion going on here: https://forums.swift.org/t/challenge-flattening-nested-optionals/24083
/// - Returns: A single-level Optional.
@rogerluan
rogerluan / CountryCodes.swift
Created January 19, 2021 08:44
Quick snippet to get all the Alpha-2 ISO country code as well as they localized display name, using native Swift lib (no imported external enums/manual entries).
var countries: [String:String] = [:]
for code in NSLocale.isoCountryCodes {
let id: String = Locale.identifier(fromComponents: [
NSLocale.Key.countryCode.rawValue : code
])
guard let name = (Locale.current as NSLocale).displayName(forKey: .identifier, value: id) else { continue }
countries[code] = name
}
for (index, countryCode) in countries.keys.sorted(by: { countries[$0]! < countries[$1]! }).enumerated() {
@rogerluan
rogerluan / Optional+Utilities.swift
Created January 26, 2021 16:14
The Optional Value Setter operator assigns a value to the receiving subject, only if the receiver is nil. Analogous to Ruby's ||= operator.
infix operator ??=
extension Swift.Optional {
/// This is the Optional Value Setter operator. It assigns a value to the receiving subject, only if
/// the receiver is `nil`.
/// When the receiver is not `nil`, the value on the right hand side won't be accessed or computed (as
/// it's an `@autoclosure`). If the receiver already has `.some` value, no value will be assigned to it,
/// thus `willSet` and `didSet` won't be called.
///
/// - SeeAlso: the rejected proposal of this feature in Swift Evolution: https://github.com/apple/swift-evolution/blob/master/proposals/0024-optional-value-setter.md
@rogerluan
rogerluan / DefMode.java
Last active April 9, 2024 19:50
A simple yet extremely effective Robocode robot.
package a20;
import robocode.*;
import java.awt.Color;
/**
* DefMode - A Robocode robot
* @author: Roger Oba
* @date: September 2014
*
* This robot won 2nd place in an internal Robocode tournament. The 1st place had a complex algorithm with over 1k LOC, and most other participants had over 600 LOC ;)
@rogerluan
rogerluan / hns-airdrop.md
Last active May 11, 2021 04:10
Github 15+ Followers HNS Airdrop

Had 15 GitHub followers in Feb 2019? You can get about US$ 2,300.00 (rate of May 11th 2021) worth of crypto for minimal effort

Is this a scam?

Hah, I won't waste much time explaining this so: tl;dr: no, it's not a scam.

There's no such thing as free money, so the money being given out here is actually a function of:

  1. Time
  2. You being probably an active Open Source contributor (after all those years, you're finally being rewarded in cash! Or, sorta cash, haha)
@rogerluan
rogerluan / remove_all_targets_except.rb
Last active August 25, 2022 22:27
Xcode doesn’t gather code coverage from packages when set to “some targets”. This script fixes this. Also see https://forums.swift.org/t/xcode-doesnt-gather-code-coverage-from-packages-when-set-to-some-targets/37220
# 1. Configure your Swift Package scheme to collect code coverage for "all targets" - NOT "some targets"
# 2. Test your Swift Package using either `xcodebuild test`, which should generate a `.xcresult` file.
# 3. Convert that `.xcresult` file into a `.json` file using:
# ```sh
# $ xcrun xccov view --report --json #{path_to_xcresult_file} > #{xccov_json_report}
# ```
# 4. Lastly, strip out the targets that you're not interested using this method.
#
# Note 1: This code considers you only want to keep 1 of the targets.
# Note 2: The resulting json file will replace/override the existing one without any warning.
@rogerluan
rogerluan / SceneDelegate.swift
Created July 23, 2021 13:02
SceneDelegate default implementation as of Xcode 12.5. Use this template to migrate apps from iOS versions prior to iOS 13, to iOS 13+
// Copyright © 2021 Roger Oba. All rights reserved.
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.