Skip to content

Instantly share code, notes, and snippets.

@alemar11
alemar11 / UIViewController+Deselection.swift
Created June 6, 2016 09:24 — forked from ZevEisenberg/LICENSE
Smoothly deselect table and collection view cells on dismissal, including interactive dismiss and interactively-partially-dismiss-then-cancel-then-dismiss-again
extension UIViewController {
func rz_smoothlyDeselectRows(tableView tableView: UITableView?) {
let selectedIndexPaths = tableView?.indexPathsForSelectedRows ?? []
if let coordinator = transitionCoordinator() {
coordinator.animateAlongsideTransitionInView(parentViewController?.view, animation: { context in
selectedIndexPaths.forEach {
tableView?.deselectRowAtIndexPath($0, animated: context.isAnimated())
}
@alemar11
alemar11 / Dictionary+KeysForValue.swift
Created June 9, 2016 07:52 — forked from ijoshsmith/Dictionary+KeysForValue.swift
Find keys mapped to a value in Swift dictionary
extension Dictionary where Value: Equatable {
/// Returns all keys mapped to the specified value.
/// ```
/// let dict = ["A": 1, "B": 2, "C": 3]
/// let keys = dict.keysForValue(2)
/// assert(keys == ["B"])
/// assert(dict["B"] == 2)
/// ```
func keysForValue(value: Value) -> [Key] {
return flatMap { (key: Key, val: Value) -> Key? in
@alemar11
alemar11 / wwdc16.md
Created July 19, 2016 10:41 — forked from mackuba/wwdc16.md
New stuff from WWDC 2016

Following the tradition from last year, here's my complete list of all interesting features and updates I could find in Apple's OSes, SDKs and developer tools that were announced at this year's WWDC. This is based on the keynotes, the "What's New In ..." presentations and some others, Apple's release notes, and blog posts and tweets that I came across in the last few weeks.

If for some reason you haven't watched the talks yet, I really recommend watching at least the "State of the Union" and the "What's New In" intros for the platforms you're interested in. The unofficial WWDC Mac app is great way to download the videos and keep track of what you've already watched.

If you're interested, here are my WWDC 2015 notes (might be useful if you're planning to drop support for iOS 8 now and start using some iOS 9 APIs).


OSX → macOS 10.12 Sierra

@alemar11
alemar11 / AndroidManifest.xml
Created January 3, 2017 16:28 — forked from chanakin/AndroidManifest.xml
Android Handle Logged In/Out State on Startup
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android">
...
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
@alemar11
alemar11 / requestForDuplicatedObjects.swift
Created February 14, 2017 12:08 — forked from onmyway133/requestForDuplicatedObjects.swift
requestForDuplicatedObjects.swift
func requestForDuplicatedObjects(moc: NSManagedObjectContext) -> NSFetchRequest {
let objectsToKeepRequest = NSFetchRequest(entityName: "Recipe")
// Fetch object IDs representing objects to keep
let ed = NSExpressionDescription()
ed.expression = NSExpression.expressionForEvaludatedObject()
ed.name = "SELF"
ed.expressionResultType = .ObjectIDAttributeType
objectsToKeepRequest.propertiesToFetch = [ed]
import Foundation
/// A type-safe Key-Value-Observer (KVO).
///
/// Extend the class to be observed to implement `KeyCodable`, e.g.
/// ```
/// extension WKWebView: KeyCodable {
/// enum Key: String {
@alemar11
alemar11 / OnboardingManager.swift
Created March 16, 2017 22:25 — forked from JohnSundell/OnboardingManager.swift
An example of using #function for user defaults properties, and a test that guards against property name changes
import UIKit
class OnboardingManager {
private let userDefaults: UserDefaults
init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}
func presentOnboardingControllerIfNeeded(in viewController: UIViewController) {
@alemar11
alemar11 / random.swift
Created March 17, 2017 16:33 — forked from rnapier/random.swift
Random numbers in Swift
import Darwin
extension Integer {
static func makeRandomValue() -> Self {
var result: Self = 0
withUnsafeMutablePointer(&result) { resultPtr in
arc4random_buf(UnsafeMutablePointer(resultPtr), sizeof(Self.self))
}
return result
}
@alemar11
alemar11 / Random.example.swift
Created March 23, 2017 11:51 — forked from pyrtsa/Random.example.swift
Sampling random numbers in Swift
import Random
random(0 ..< 10) // Int
random(1 ... 1000_000) // Int
random(-1000_000 ... 1000_000) // Int
random(Int.min ... Int.max) // Int
randomMax(UInt64.max) // UInt64
random(0 ... UInt64.max) // UInt64
random(1 ... UInt(10)) // UInt
@alemar11
alemar11 / PhonecallReceiver.java
Created March 24, 2017 09:12 — forked from ftvs/PhonecallReceiver.java
Detecting an incoming call coming to an Android device. Remember to set the appropriate permissions in AndroidManifest.xml as suggested in the Stackoverflow link. Usage example in comments. Source: http://stackoverflow.com/a/15564021/264619 Explanation: http://gabesechansoftware.com/is-the-phone-ringing/#more-8
package com.gabesechan.android.reusable.receivers;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
public abstract class PhonecallReceiver extends BroadcastReceiver {