GithubHelp home page GithubHelp logo

woshilaozhang / swifterswift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swifterswift/swifterswift

0.0 1.0 0.0 6.51 MB

A handy collection of more than 500 native Swift 3 extensions to boost your productivity.

Home Page: https://swifterswift.com

License: MIT License

Swift 98.65% Objective-C 0.17% Ruby 0.68% Shell 0.49%

swifterswift's Introduction

Build Status Cocoapods Carthage compatible codecov CocoaPods CocoaPods Platform Swift Xcode MIT Slack Status

SwifterSwift is a collection of over 500 native Swift 3 extensions, with handy methods, syntactic sugar, and performance improvements for wide range of primitive data types, UIKit and Cocoa classes –over 500 in 1– for iOS, macOS, tvOS and watchOS.

v1.6.3 brings more than 20 new extensions, drastically improved tests and minor bug fixes

Requirements:

  • iOS 8.0+ / tvOS 9.0+ / watchOS 3+ / macOS 10.10+
  • Xcode 8.1+
  • Swift 3.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate SwifterSwift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'SwifterSwift'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate SwifterSwift into your Xcode project using Carthage, specify it in your Cartfile:

github "SwifterSwift/SwifterSwift" ~> 1.5

Run carthage update to build the framework and drag the built SwifterSwift.framework into your Xcode project.

Swift Package Manager

You can use The Swift Package Manager to install SwifterSwift by adding the proper description to your Package.swift file:

import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/SwifterSwift/SwifterSwift.git", majorVersion: 1),
    ]
)

Note that the Swift Package Manager is still in early design and development, for more information checkout its GitHub Page

Manually

Add the extensions folder to your Xcode project to use all extensions, or a specific extension.


Read the Full Documentation

Full documentation with code examples is available in the Docs repo


List of Extensions

List of UIKit Extensions

List of Cocoa Extensions


How cool is this?

SwifterSwift is a library of over 500 properties and methods, designed to extend Swift's functionality and productivity, staying faithful to the original API design guidelines of Swift 3.

Here are some examples:

Array Extensions:

// Remove duplicates from an array
[1, 2, 3, 1, 3].removeDuplicates() -> [1, 2, 3]

// Return all indexes of specified item
["h", "e", "l", "l", "o"].indexes(of: "l") -> [2, 3]

// Shuffle an array
["h", "e", "l", "l", "o"].shuffled = ["e", "l", "o", "l", "h"]

// Return random item from an array
[1, 2, 3, 4, 5].randomItem -> 3

// and many others!

Check All Array Extensions!


Date Extensions:

// Get and set components from date with ease
date.hour = 14

// Check if date is in today
Date().isInToday -> true

// Add 1 month to current date
Date().add(.month, value: 1)

// Return date at the beginning of current day
Date().beginning(of: .day)

// Return date at the end of current month
Date().end(of: .month)

// Check if date is in current calendar unit
Date().isInCurrent(.month) -> true

// Return iso8601 string for date
Date().iso8601String -> "2016-08-23T21:26:15.287Z"

// Create date from iso8601 string
let date = Date(iso8601String: "2016-08-23T21:26:15.287Z")

// Create date from DateComponents
let date = Date(year: 2016, month: 8, day: 15) // other components set to current
let date = Date(hour: 9, minute: 18, second: 1) // other components set to current

// Represent date as a string with ease
Date().dateString(ofStyle: .medium) -> "Aug 26, 2016"
Date().timeString(ofStyle: .short) -> "12:55 AM"
Date().dateTimeString() -> "Aug 26, 2016, 12:55:24 AM"

// Get day name or month name from a date
Date().dayName(ofStyle: .full) -> "Sunday"
Date().monthName(ofStyle: .threeLetters) -> "Dec"

// and many others!

Check All Date Extensions!


String Extensions:

// Return count of substring in string
"hello world".count(of "o", caseSensitive: false) -> 2

// Return an array of strings separated by given string
"hello world".split(by: " ") -> ["hello", "world"]

// Return string with no spaces or new lines in beginning and end
"\n Hello   ".trimmed -> "Hello"

// Return most common character in string
"swifterSwift is making swift more swifty".mostCommonCharacter -> "i"

// Returns CamelCase of string
"Some variable nAme".camelCased -> "someVariableName"

// Check if string is in valid email format
"[email protected]".isEmail -> true

// Check if string contains at least one letter and one number
"123abc".isAlphaNumeric -> true

// Reverse string
"123abc".reverse() -> "cba321"

// Return latinized string
"Hèllö Wórld!".latinize() -> "Hello World!"

// Create random string of length
String.random(ofLength: 10) -> "AhEju28kNl"

// Check if string contains one or more instance of substring
"Hello World!".contain("o", caseSensitive: false) -> true

// Check if string contains one or more emojis
"string👨‍with😍emojis✊🏿".containEmoji -> true

// Subscript strings easily
"Hello"[2] = "l"

// Slice strings
let s = "Hello world"
s.slicing(from: 6, length: 5) // -> "world"
s.slicing(from: 6, to: 11) // -> "world"
s.slicing(at: 6) // -> "world"

// Convert string to numbers
"12.12".toDouble -> 12.12

// Encode and decode URLs
"it's easy to encode strings".urlEncoded -> "it's%20easy%20to%20encode%20strings"
"it's%20easy%20to%20encode%20strings".urlDecoded -> "it's easy to encode strings"

// Encode and decode base64
"Hello World!".base64Encoded -> "SGVsbG8gV29ybGQh"
"SGVsbG8gV29ybGQh".base64Decoded = "Hello World!"

// Truncate strings with a trailing
"This is a very long sentence".truncated(toLength: 14, trailing: = "...") -> "This is a very..."

// Repeat a string n times
"s" * 5 -> "sssss"

// NSString has never been easier
let boldString = "this is string".bold.colored(with: .red)

// and many others!

Check All String Extensions!


Dictionary Extensions:

let dict = ["id": 1, "Product-Name": "SwifterSwift"]

// Check if key exists in dictionary.
dict.has(key: "id") -> true

// Lowercase all keys in dictionary.
dict.lowercaseAllKeys() -> ["id": 1, "product-name": "SwifterSwift"]

// Create JSON Data and string from a dictionary
let json = dict.jsonString(prettify: true)

// and many others!

Check All Dictionary Extensions!


Number Types Extensions:

// Return square root of a number
 9 = 3

// Return square power of a number
5 ** 2 = 25

// Return a number plus or minus another number
5 ± 2 = (3, 7)

// Return random number in range
Int.randomBetween(min: 1, max: 10) = 6

// Return roman numeral for a number
134.romanNumeral = "CXXXIV"

// and many others!

Check All Extensions!


UIKit Extensions

SwifterSwift has many great extensions for UIKit also:

UIColor Extensions:

// Create new UIColor for RGB values
let color = UIColor(red: 121, green: 220, blue: 164)

// Create new UIColor for a hex string (including strings starting with #, 0x or in short css hex format)
let color = UIColor(hexString: "#00F")

// Create new UIColor for a hexadecimal value
let color = UIColor(hex: 0x45C91B)

// Blend two colors with ease
UIColor.blend(UIColor.red, intensity1: 0.5, with: UIColor.green, intensity2: 0.3)

// Return hexadecimal value string
UIColor.red.hexString -> "#FF0000"

// Return short hexadecimal value string
UIColor(hex: #00ffff) -> "#0FF"

// Use Google Material design colors with ease
let indigo = UIColor.material.indigo

// Use CSS colors with ease:
let beige = UIColor.css.beige

// Return brand colors from more than 30 social brands
let facebookColor = UIColor.social.facebook

// and many others!

Check UIColor Extensions!


UIView Extensions

// Set borderColor, borderWidth, cornerRadius, shadowColor, and many other properties from code or storyboard
view.cornerRadius = 30

// Set some or all corners radiuses of view.
view.roundCorners([.bottomLeft, .topRight], radius: 30)


// Add shadow to view
view.addShadow(ofColor .black, radius: 3, opacity: 0.5)

// Animate view with completion
view.fadeIn(duration: 1, completion:((Bool) -> Void)?)
view.fadeOut(duration: 1, completion:((Bool) -> Void)?)
view.rotate(byAngle 90, ofType type: .degrees, animated: true, duration: 1, completion: { print("done") })
view.rotate(toAngle -3, ofType type: .radians, animated: false, duration: 1, completion: nil)
view.scale(byOffset: 4, animated: true, duration:1)
view.shake(direction: .horizontal, duration: 1, animationType: .easeOut)

// save screenshot of a view
let image = view.screenShot

// and many others!

Check UIView Extensions!


UIAlertController Extensions

// Create a new alert controller from string or Error
let alert = UIAlertController(title: "Couldn't sign in", message: "Invalid username or password!")
let alert = UIAlertController(title: "Error", error: Error)

// show alert with ease
alert.show()

// and many others!

Check UIAlertController Extensions!


UIButton Extensions

// Set title, title color and image for all states at once!
button.setTitleForAllStates("Login")
button.setTitleColorForAllStates(UIColor.blue)
button.setImageForAllStates(UIImage(named: "login"))

// or set each of them from code or storyboard
button.titleForHighlighted = "Login"

// and many others!

Check UIButton Extensions!


UIImage Extensions

// Crop images
let croppedImage = image.cropped(to CGRect)

// Create UIImage from color
let image = UIImage(color: UIColor, size: CGSize)

// scale to fit width or height
let scaledImage = image.scaled(toHeight: CGFloat)
let scaledImage = image.scaled(toWidth: CGFloat)

// Compress images
let compressedImage = image.compressd(quality: 0.3)

// get image size
image.kilobytesSize = 114

// and many others!

Check UIImage Extensions!


UIImageView Extensions

// Download an image from URL in background
imageView.download(from url, contentMode: .scaleAspectFit, placeHolder: UIImage?)

// Blur image view
imageView.blur(withStyle: .light)

// and many others!

Check UIImageView Extensions!


UINavigationBar Extensions

// Change navigation bar font and color
navbar.setTitleFont(UIFont, with color: UIColor.black)

// and many others!

Check UINavigationBar Extensions!


UINavigationController Extensions

// Pop ViewController with completion handler.
navController.popViewController(completion: (()->Void)?)

// Push ViewController with completion handler.
navController.pushViewController(UIViewController, completion: (()->Void)?)

// and many others!

Check UINavigationController Extensions!


UITableView Extensions

// Return index path for last row in section.
tableView.indexPathForLastRow(in section: 2)

// Scroll to bottom or top of TableView.
tableView.scrollToBottom(animated: true)
tableView.scrollToTop(animated: true)

// and many others!

Check All UIKit Extensions!


Cocoa Extensions

SwifterSwift has many great extensions for Cocoa too:

CGPoint Extensions

// Get distance from another CGPoint.
point1.distance(from point2) = 12.45

Multiply a CGPoint with a scalar
result = 5 * point

// and many others!

Check All Cocoa Extensions!


Misc Extensions

// Check if app is running in debugging mode
SwifterSwift.isInDebuggingMode

// Check if app is running on simulator
SwifterSwift.isRunningOnSimulator

// Detect screenshots
SwifterSwift.didTakeScreenShot {
	print("User did take a screenshot!")
}

// and many others!

Check SwifterSwift Extensions!


Get involved:

Your feedback is always appreciated and welcomed. Please refer to contributing guidelines before participating.


Thanks:

Special thanks to:

swifterswift's People

Contributors

omaralbeik avatar sd10 avatar 0mars avatar pawurb avatar lucianopalmeida avatar pvtmert avatar basememara avatar ecylo avatar joerocca avatar ythecombinator avatar spacelatte avatar emiroks avatar gperdomor avatar hujunfeng avatar quaggie avatar ronanrodrigo avatar sairamkotha avatar shivahuang avatar gitter-badger avatar yhwinnie avatar mudox avatar king6cong avatar

Watchers

xiaodiaosi 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.