Swift Literal Convertibles for Foundation
Swift
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
Literally.swift
README.md

README.md

Literally

Swift Literal Convertibles for Foundation

Changes in Swift 1.1 and its associated source mappings to Foundation have unintentionally broken the initializer patterns necessary for this project to work [1][2]. Hopefully, this will be resolved in a future release of Swift.

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.

Installation

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.


Inventory

ArrayLiteralConvertible

  • NSCharacterSet
  • NSCountedSet (subclass of NSMutableSet)
  • NSIndexPath
  • NSIndexSet
  • NSOrderedSet
  • NSSet

IntegerLiteralConvertible

  • NSColor (OS X)
  • UIColor (iOS)

NilLiteralConvertible

  • NSNull

StringLiteralConvertible

  • NSCharacterSet
  • NSExpression
  • NSPredicate
  • NSRegularExpression
  • NSScanner
  • NSTimeZone
  • NSURL
  • UIImage (iOS)

Usage

NSCharacterSet

let characterSetFromArray: NSCharacterSet = ["$", "", "¥", "£"]
characterSetFromArray.bitmapRepresentation

let characterSetFromString: NSCharacterSet = "aeiouy"
characterSetFromString.bitmapRepresentation

NSColor

let gunmetal: NSColor = 0x292E37

NSCountedSet

let countedSet: NSCountedSet = [1, 2, 2, 3, 3, 3]
countedSet.countForObject(2)

NSExpression

let expression: NSExpression = "4 + 4"
expression.operand

NSIndexPath

let indexPath: NSIndexPath = [3,17]
indexPath.indexAtPosition(1)

NSIndexSet

let indexSet: NSIndexSet = [1, 2, 3, 4, 5]
indexSet.containsIndex(4)

NSNull

let null: NSNull = nil

NSPredicate

let predicate: NSPredicate = "value <> 'foo'"

NSRegularExpression

let regex: NSRegularExpression = "foo"
regex.numberOfMatchesInString("foobar", options: nil, range: NSMakeRange(0, 6))

NSScanner

let scanner: NSScanner = "foo: 1, bar: 2, baz: 3"
var foo: NSString?
scanner.scanUpToString(":", intoString: &foo)
foo!

NSSet

let set: NSSet = [1, 2, 3]
set.containsObject(3)

NSTimeZone

let timeZone: NSTimeZone = "America/Los_Angeles"
timeZone.secondsFromGMTForDate(NSDate())

NSURL

let url: NSURL = "http://github.com/mattt/Literally"
url.host!

UIColor

let puce: UIColor = 0x722F37

UIImage

let image: UIImage = "image.png"

Contact

Mattt Thompson

License

Literally is available under the MIT license. See the LICENSE file for more info.