Codemine 0.1.0

Codemine 0.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2016
SPMSupports SPM

Maintained by Nodes Agency.



Codemine 0.1.0

Codemine

Codemine is a collection of extensions containing useful functions and syntactic sugar for your Swift project.

📝 Requirements

  • iOS 8.0+

📦 Installation

Swit Package Manager

To use Codemine as a Swift Package Manager package just add the following to your Package.swift file.

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .Package(url: "https://github.com/nodes-ios/Codemine.git", majorVersion: 0)
    ]
)

NOTE: This doesn’t currently work as SPM doesn’t support iOS, but once it will we will already be supporting it! :)

💻 Usage

Application

let appName = Application.name             // CFBundleDisplayName : String
let appVersion = Application.version       // CFBundleShortVersionString : String
let appExecutable = Application.executable // CFBundleExecutable : String
let appBundle = Application.bundle         // CFBundleIdentifier : String
let appSchemes = Application.schemes       // CFBundleURLSchemes : [String]
let mainAppScheme = Application.mainScheme // CFBundleURLSchemes.first : String?

Gain easy access to main bundle information.

CGPoint

let point1 = CGPoint(x: 5, y: 5)
let point2 = CGPoint(x: 5, y: 6)
print(point1.isCloseTo(point2, tolerance: 1))   // true
print(point1.isCloseTo(point2, tolerance: 0.5)) // false
print(point1+point2)    // (10, 11)
print(point1*point2)    // (25, 30)
print(point1*2)         // (10, 10)
print(point1-point2)    // (0, -1)
print(point1/point2)    // (1, 0.83)
print(point1/5)         // (1, 1)

CGRect

var rect = CGRect(x: 10, y: 10, width: 120, height: 100)
rect.x = 50
print(rect) // outputs x:50, y:20, width: 120, height:100
rect.y = -10
print(rect) // outputs x:50, y:-10, width: 120, height:100
let reversedRect = rect.rectByReversingSize()
print(reversedRect) // outputs x:50, y:-10, width:100, height:120

CGSize

let size1 = CGSize(width: 20, height: 40)
let size2 = CGSize(width: 121, height: 576)
print(size1+size2)      // CGSize(width: 141, height: 616)

Grand Central Dispatch

dispatch {
  // dispatch in main queue
}

dispatch(queue: .Background) {
  // dispatch in background queue
}

lazy var serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL)
dispatch(queue: .Custom(serialQueue)) {
  // dispatch in a serial queue
}

Easy dispatching with grand central dispatch. Support all the regular global queues: Main, Interactive, Initiated, Utility, Background. And .Custom() for your own dispatch queues.

NSError

let error = NSError(domain: domain, code: code, description: description)

instead of

let error = NSError(domain: domain, code: code, userInfo: [NSLocalizedDescriptionKey : description])

NSURL

guard let url = NSURL(string: "https://example.com/image.png") else { return }
let size = CGSize(width: 512, height: 256)
let heightParameterName = "height"
let widthParameterName = "width"

let url2 = url.urlByAppendingAssetSize(size, mode: .Default, heightParameterName: heightParameterName, widthParameterName: widthParameterName)
print(url2.absoluteString)  // on an @2x screen: "https://example.com/image.png?width=1024&height=512"

This method appends the size multiplied by UIScreen.mainScreen().scale to an asset url so that the asset has the correct size to be shown on the screen.

String

let camelCaseStr1 = "userId"
let camelCaseStr2 = "isUserActiveMemberOfCurrentGroup"

print(camelCaseStr1.camelCaseToUnderscore())    // "user_id"
print(camelCaseStr2.camelCaseToUnderscore())    // "is_user_active_member_of_current_group"
"[email protected]".isValidEmailAddress()   // true
"email.example.com".isValidEmailAddress()   // false
let str = "Hello world!"
let range = str.rangeFromString("e", toString: " w")    // Range(1..<7)

UIColor

let red = UIColor(rgb: 0xFF0000)

UIImage

let image = UIImage.imageFromColor(UIColor.redColor(), CGSize(width: 512, height: 256), cornerRadius:4.0)

Returns a UIImage filled with red color, of the specified size and with the specified corner radius

let image = UIImage.imageByEmbeddingIconIn(UIImage(named:"profilePhoto"), icon: UIImage(named:"favouriteIcon"))

Returns a UIImage composed by overlaying the icon on top of the first image.

UIView

let view = UIView.viewWithNibNamed("customView")

Returns a view instantiated from the specified nib.

let view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
view.roundViewCorners(UIRectCorner.AllCorners, radius: 4.0)

Rounds the specified corners of a UIView to the specified radius.

Then

let UIView().then {
  $0.backgroundColor = UIColor.blackColor()
}

👥 Credits

Made with ❤️ at Nodes.

Some functions & tweaks were borrowed from Hyper’s Sugar 🙈.

📄 License

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