Skip to content

Instantly share code, notes, and snippets.

let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi enim lacus, ullamcorper in gravida a, semper id dolor. Mauris quis metus id"
extension String {
public func split(
maxSplit: Int = .max,
allowEmptySlices: Bool = false,
@noescape isSeparator: ((Character) throws -> Bool)) rethrows -> [String] {
return try self.characters.split(
maxSplit,
0. Read the <link>FAQ</link>. Other *mandatory* reading follows at the bottom of these rules.
1. Google first, ask second. You must work through the Rubber Duck process <i>before</i> asking questions in-channel.
2. This is a volunteer-staffed channel. The time of the volunteers is worth at *least* as much as your time. We are not your employees or your servants. Clean your own room, do your own homework, write your own code, and don't forget to say "Thank you" for the help.
3. The phrase "doesn't work" isn't allowed. Prepare to explain the problem without links to Stack Overflow.
4. Show your code. We don't care about your NDAs. If you don't want to show your code, go pay a consultant. We may ask you to create a minimal project that demonstrates your issue. Save time by doing so before entering the channel.
@erica
erica / gist:481a34bc2a6f838dcdbe
Created June 23, 2014 16:58
Xcode seems to hate this
import Foundation
struct BagGenerator<T:Hashable> : Generator
{
typealias Element = (T, Int, Int)
var count = 0
var _backingGenerator : DictionaryGenerator<T, Int>
init(_ backingDictionary : Dictionary<T, Int>) {
_backingGenerator = backingDictionary.generate()
}
//
// Bag.swift
// CmdLineTest
//
// Created by Erica Sadun on 6/23/14.
// Copyright (c) 2014 Erica Sadun. All rights reserved.
//
import Foundation
import Cocoa
// See http://www.ruby-doc.org/core-2.1.1/Enumerable.html
extension Array {
var first : Element? {return self.count > 0 ? self[0] : nil}
var last : Element? {return self.count > 0 ? self[self.count - 1] : nil}
var random : Element? {return self.count > 0 ? self[Int(arc4random_uniform(UInt32(self.count)))] : nil}
protocol AbsoluteValuable
protocol ArrayBound
protocol ArrayBufferType
protocol ArrayLiteralConvertible
protocol ArrayType
protocol BidirectionalIndex
protocol BitwiseOperations
protocol CVarArg
protocol CharacterLiteralConvertible
protocol Collection
#! /bin/sh
# All credit to @mikeash
echo "import Cocoa\n:print_decl $1" | /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -integrated-repl | open -f
let fd = open("/tmp/scratch.txt", O_WRONLY|O_CREAT, 0o666)
if fd < 0 {
perror("could not open /tmp/scratch.txt")
} else {
let text = "Hello World\n"
write(fd, text, strlen(text))
close(fd)
}
let fname = "/tmp/scratch.txt"
let fptr = fopen(fname, "w")
if (fptr == nil) {
let localErrno = errno
println("Unable to open \(fname): \(strerror(localErrno))")
} else {
let text = "Hello World\n"
let bytesWritten = fwrite(text, UInt(sizeof(Byte)), strlen(text), fptr)
println("Wrote \(bytesWritten) bytes")
fclose(fptr)
@erica
erica / gist:e4b3fca1eba67536e4a3
Last active August 29, 2015 14:04
Which one?
func ErrorString1(error : NSError?) -> String {
if let theError = error as? NSError {
if let description = theError.localizedDescription {
return description
}
}
return ""
}
func ErrorString2(error_ : NSError?) -> String {