Swift, in contrast with Objective-C, makes a distinction between values that may be nil
and values that can never be nil
through its use of Optionals. Due to the fact that Objective-C does not make this distinction, Objective-C functions that do not use the Nullability annotations are imported with parameters of the implicitly unwrapped optional type. Unfortunately, this allows users to write their own Swift code that looks like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you haven't already, download Conky Google Now from http://satya164.deviantart.com/art/Conky-Google-Now-366545753 and unzip it to your home folder; then copy this .conkyrc over the one you just extracted | |
# Requires lm-sensors and inxi (and conky) to be installed: | |
# sudo apt-get install lm-sensors inxi | |
# Adapted from these: | |
# http://satya164.deviantart.com/art/Conky-Google-Now-366545753 | |
# https://gist.github.com/anonymous/6666594 | |
# Conky Google Now style # |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
if [ $# == 1 ]; then | |
git fetch origin pull/$1/head:pr-$1 | |
git checkout pr-$1 | |
elif [ $# == 2 ]; then | |
git fetch $1 pull/$2/head:pr-$2 | |
git checkout pr-$2 | |
else | |
echo "Usage: git test-pr [remote] pr#" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
# Make sure this runs on a modern bash with support for ;;& in case | |
shopt -s extglob | |
# Make sure we're in a git directory | |
if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then | |
# Print a leading space to separate from the rest of the PS1 | |
echo -n " " | |
# Check our status relative to upstream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol SockAddr { } | |
extension sockaddr_in: SockAddr { } | |
extension sockaddr_in6: SockAddr { } | |
enum SocketAddress: CustomDebugStringConvertible { | |
case ipv4(sockaddr_in) | |
case ipv6(sockaddr_in6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd() { | |
# Set the current directory to the 0th history item | |
cd_history[0]=$PWD | |
if [[ $1 == -h ]]; then | |
for i in ${!cd_history[@]}; do | |
echo $i: "${cd_history[$i]}" | |
done | |
return | |
elif [[ $1 =~ ^-[0-9]+ ]]; then | |
builtin cd "${cd_history[${1//-}]}" || # Remove the argument's dash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function postUpdatedxkcdIfNecessary() { | |
var properties = PropertiesService.getUserProperties(); | |
var latestComic = JSON.parse(UrlFetchApp.fetch("http://xkcd.com/info.0.json").getContentText()); | |
if (latestComic["num"] > properties.getProperty("lastComic")) { | |
var title = latestComic["title"]; | |
var imageURL = latestComic["img"]; | |
var altText = latestComic["alt"]; | |
var number = latestComic["num"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let handle = dlopen("/System/Library/PrivateFrameworks/Calculate.framework/Calculate", RTLD_LAZY) | |
let CalculatePerformExpression = unsafeBitCast(dlsym(handle, "CalculatePerformExpression"), to: | |
(@convention(c) (UnsafePointer<CChar>, Int, Int, UnsafePointer<CChar>) -> Bool).self) | |
let expression = Array(CommandLine.arguments.dropFirst()).joined(separator: " ") | |
let answer = Array<CChar>(repeating: 0, count: 100) | |
if CalculatePerformExpression(expression, 16, 1, answer) { | |
print(String(cString: answer)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Logic necessary to implement a "closing handler" for a NSDocument | |
// | |
// This is inspired by: | |
// - https://stackoverflow.com/questions/49343307/best-practice-to-implement-canclosewithdelegateshouldclosecontextinfo | |
// - https://github.com/keith/radars/blob/master/NSDocumentSwiftAnnotations/NSDocumentSwiftAnnotations/Document.swift | |
// Keith's supporting documentation for his radar submission: http://www.openradar.me/33422662 | |
// Documentation: | |
// - https://developer.apple.com/library/content/releasenotes/AppKit/RN-AppKitOlderNotes/ | |
// Paragraph called: "Advice for Overriders of Methods that Follow the delegate:didSomethingSelector:contextInfo: Pattern" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
int overridden_strncasecmp(const char *s1, const char *s2, size_t n) { | |
if (n != 6 || strncmp(s2, "bright", n)) { | |
return strncasecmp(s1, s2, n); | |
} else { | |
return !0; | |
} | |
} |
OlderNewer