GithubHelp home page GithubHelp logo

literally's Introduction

Literally

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.

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.

literally's People

Contributors

mattt avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.