Swift Literal Convertibles for Foundation
In Swift, literals can be used to provide convenient shorthand initializers for any object conforming to the corresponding protocol. For example, an NSURL
can be initialized from a string literal (let url: NSURL = "http://example.com"
) if NSURL
is extended to conform to the StringLiteralConvertible
protocol (which implements convertFromStringLiteral
).
Literally is a demonstration of how Foundation framework classes can be extended to take advantage of this language feature. Best efforts have been made to find useful opportunities, while forgoing ones that could technically work, but would not yield any benefit (e.g. conforming NSMapTable
to DictionaryLiteralConvertible
, since it would not be able to specify storage options).
This is not recommended for usage in production code. In time, it is expected that Cocoa and the Swift standard library will coalesce in significant ways, making this kind of adaptation irrelevant.
For more information about literal convertibles in Swift, check out this NSHipster article.
The infrastructure and best practices for distributing Swift libraries is currently being developed by the developer community during this beta period of the language and Xcode. In the meantime, you can simply add Literally as a git submodule, and drag the Literally.swift
file into your Xcode project.
NSCharacterSet
NSCountedSet
(subclass ofNSMutableSet
)NSIndexPath
NSIndexSet
NSOrderedSet
NSSet
NSNull
NSCharacterSet
NSExpression
NSPredicate
NSRegularExpression
NSScanner
NSTimeZone
NSURL
let characterSetFromArray: NSCharacterSet = ["$", "€", "¥", "£"]
characterSetFromArray.bitmapRepresentation
let characterSetFromString: NSCharacterSet = "aeiouy"
characterSetFromString.bitmapRepresentation
let countedSet: NSCountedSet = [1, 2, 2, 3, 3, 3]
countedSet.countForObject(2)
let expression: NSExpression = "4 + 4"
expression.operand
let indexPath: NSIndexPath = [3,17]
indexPath.indexAtPosition(1)
let indexSet: NSIndexSet = [1, 2, 3, 4, 5]
indexSet.containsIndex(4)
let null: NSNull = nil
let predicate: NSPredicate = "value <> 'foo'"
let regex: NSRegularExpression = "foo"
regex.numberOfMatchesInString("foobar", options: nil, range: NSMakeRange(0, 6))
let scanner: NSScanner = "foo: 1, bar: 2, baz: 3"
var foo: NSString?
scanner.scanUpToString(":", intoString: &foo)
foo!
let set: NSSet = [1, 2, 3]
set.containsObject(3)
let timeZone: NSTimeZone = "America/Los_Angeles"
timeZone.secondsFromGMTForDate(NSDate())
let url: NSURL = "http://github.com/mattt/Literally"
url.host!
Mattt Thompson
Literally is available under the MIT license. See the LICENSE file for more info.