Swift开源:SwifterSwift - 收集370多个Swift3扩展

279834930 8年前
   <p style="text-align:center"><img src="https://simg.open-open.com/show/d8e79eb600023be1c6f8c062762739fe.png"></p>    <p>A handy collection of <strong>more than 370 native Swift 3 extensions</strong> to boost your productivity.</p>    <h2><a href="/misc/goto?guid=4959729313563943696" rel="nofollow,noindex">Whats New in v1.3?</a></h2>    <p>v1.3 adds <strong>more than 90 new extensions</strong> making it the widest extensions library available online for Swift 3 with extensions for more than <strong>36 type</strong> . This is the biggest update since library launch! we're so excited ��</p>    <h2>Requirements:</h2>    <p>Xcode 8 (or later) with Swift 3. This library is made for iOS 8 or later, however most of the extensions should work on watchOS, tvOS, and macOS</p>    <h2>Installation</h2>    <h3>CocoaPods</h3>    <p>CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:</p>    <pre>  <code class="language-swift">$ gem install cocoapods</code></pre>    <p>To integrate SwifterSwift into your Xcode project using CocoaPods, specify it in your Podfile :</p>    <pre>  <code class="language-swift">source 'https://github.com/CocoaPods/Specs.git'  platform :ios, '8.0'  use_frameworks!    target '<Your Target Name>' do      pod 'SwifterSwift'  end</code></pre>    <p>Then, run the following command:</p>    <pre>  <code class="language-swift">$ pod install</code></pre>    <h3>Carthage</h3>    <p>Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.</p>    <p>You can install Carthage with Homebrew using the following command:</p>    <pre>  <code class="language-swift">$ brew update  $ brew install carthage</code></pre>    <p>To integrate SwifterSwift into your Xcode project using Carthage, specify it in your Cartfile :</p>    <pre>  <code class="language-swift">github "omaralbeik/SwifterSwift" ~> 1.3.4</code></pre>    <p>Run carthage update to build the framework and drag the built SwifterSwift.framework into your Xcode project.</p>    <h3>Manually</h3>    <p>Add theextensions folder to your Xcode project to use all extensions, or a specific extension.</p>    <h2>How to contribute:</h2>    <p>SwifterSwift is in its early stages, any feedback is appreciated and welcomed. Please refer to the contributing guidelines before participating.</p>    <h2>List Of Extensions</h2>    <ul>     <li><a href="/misc/goto?guid=4959729313653801580" rel="nofollow,noindex">Array extensions</a></li>     <li><a href="/misc/goto?guid=4959729313735777726" rel="nofollow,noindex">Bool extensions</a></li>     <li><a href="/misc/goto?guid=4959729313816773687" rel="nofollow,noindex">Character extensions</a></li>     <li><a href="/misc/goto?guid=4959729313900466483" rel="nofollow,noindex">Data extensions</a></li>     <li><a href="/misc/goto?guid=4959729313980154878" rel="nofollow,noindex">Date extensions</a></li>     <li><a href="/misc/goto?guid=4959729314060442875" rel="nofollow,noindex">Dictionary extensions</a></li>     <li><a href="/misc/goto?guid=4959729314144544541" rel="nofollow,noindex">Double extensions</a></li>     <li><a href="/misc/goto?guid=4959729314231121840" rel="nofollow,noindex">Float extensions</a></li>     <li><a href="/misc/goto?guid=4959729314302248785" rel="nofollow,noindex">Int extensions</a></li>     <li><a href="/misc/goto?guid=4959729314385977150" rel="nofollow,noindex">String extensions</a></li>     <li><a href="/misc/goto?guid=4959729314466151382" rel="nofollow,noindex">SwifterSwift</a></li>     <li><a href="/misc/goto?guid=4959729314544542410" rel="nofollow,noindex">CGFloat extensions</a></li>     <li><a href="/misc/goto?guid=4959729314624869032" rel="nofollow,noindex">CGPoint extensions</a></li>     <li><a href="/misc/goto?guid=4959729314727027309" rel="nofollow,noindex">CGSize extensions</a></li>     <li><a href="/misc/goto?guid=4959729314800165984" rel="nofollow,noindex">NSAttributedString extensions</a></li>     <li><a href="/misc/goto?guid=4959729314886443352" rel="nofollow,noindex">UIAlertController extensions</a></li>     <li><a href="/misc/goto?guid=4959729314967018783" rel="nofollow,noindex">UIBarButtonItem extensions</a></li>     <li><a href="/misc/goto?guid=4959729315042924036" rel="nofollow,noindex">UIButton extensions</a></li>     <li><a href="/misc/goto?guid=4959729315119586459" rel="nofollow,noindex">UICollectionView extensions</a></li>     <li><a href="/misc/goto?guid=4959729315205001862" rel="nofollow,noindex">UIColor extensions</a></li>     <li><a href="/misc/goto?guid=4959729315289596407" rel="nofollow,noindex">UIImage extensions</a></li>     <li><a href="/misc/goto?guid=4959729315361968515" rel="nofollow,noindex">UIImageView extensions</a></li>     <li><a href="/misc/goto?guid=4959729315442107154" rel="nofollow,noindex">UILabel extensions</a></li>     <li><a href="/misc/goto?guid=4959729315523305987" rel="nofollow,noindex">UINavigationBar extensions</a></li>     <li><a href="/misc/goto?guid=4959729315604703128" rel="nofollow,noindex">UINavigationController extensions</a></li>     <li><a href="/misc/goto?guid=4959729315679039568" rel="nofollow,noindex">UINavigationItem extensions</a></li>     <li><a href="/misc/goto?guid=4959729315765027898" rel="nofollow,noindex">UISearchBar extensions</a></li>     <li><a href="/misc/goto?guid=4959729315850522585" rel="nofollow,noindex">UISegmentedControl extensions</a></li>     <li><a href="/misc/goto?guid=4959729315927117855" rel="nofollow,noindex">UISlider extensions</a></li>     <li><a href="/misc/goto?guid=4959729316008538760" rel="nofollow,noindex">UISwitch extensions</a></li>     <li><a href="/misc/goto?guid=4959729316083509629" rel="nofollow,noindex">UITabBar extensions</a></li>     <li><a href="/misc/goto?guid=4959729316166282603" rel="nofollow,noindex">UITableView extensions</a></li>     <li><a href="/misc/goto?guid=4959729316253789649" rel="nofollow,noindex">UITextField extensions</a></li>     <li><a href="/misc/goto?guid=4959729316326196774" rel="nofollow,noindex">UITextView extensions</a></li>     <li><a href="/misc/goto?guid=4959729316414211556" rel="nofollow,noindex">UIView extensions</a></li>     <li><a href="/misc/goto?guid=4959729316483923728" rel="nofollow,noindex">UIViewController extensions</a></li>    </ul>    <h2>How cool is this?</h2>    <p>SwifterSwift is a library of over <strong>370 properties and methods</strong> , designed to extend Swift's functionality and productivity, staying faithful to the original API design guidelines of swift 3.</p>    <p>Here are some examples:</p>    <p>Array Extensions:</p>    <pre>  <code class="language-swift">// Remove duplicates from 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 array  ["h", "e", "l", "l", "o"].shuffled = ["e", "l", "o", "l", "h"]    // Return random item from array  [1, 2, 3, 4, 5].randomItem -> 3    // and many others!</code></pre>    <p>Date Extensions:</p>    <pre>  <code class="language-swift">// 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"    // and many others!</code></pre>    <p>String Extensions:</p>    <pre>  <code class="language-swift">// 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  "omaralbeik@gmail.com".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:man:‍with:heart_eyes:emojis:fist|type_6:".containEmoji -> true    // 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!</code></pre>    <p>Dictionary Extensions:</p>    <pre>  <code class="language-swift">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!</code></pre>    <p>Number Types Extensions:</p>    <pre>  <code class="language-swift">// 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!</code></pre>    <h2>UI Extensions</h2>    <p>SwifterSwift has many great UI extensions:</p>    <p>UIColor Extensions:</p>    <pre>  <code class="language-swift">// Create new UIColor for RGB values  let color = UIColor(red: 121, green: 220, blue: 164)    // 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"    // Use Google Material design colors with ease  let indigo = UIColor.material.indigo    // Return brand colors from more than 30 social brands  let 非死bookColor = UIColor.social.非死book    // and many others!</code></pre>    <p>UIView Extensions</p>    <pre>  <code class="language-swift">// 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)</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c0953055f986ab55a718be7fb3fe5331.png"></p>    <pre>  <code class="language-swift">// 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!</code></pre>    <p>CGPoint Extensions</p>    <pre>  <code class="language-swift">// Get distance from another CGPoint.  point1.distance(from point2) = 12.45    Multiply a CGPoint with a scalar  result = 5 * point    // and many others!</code></pre>    <p>UIAlertController Extensions</p>    <pre>  <code class="language-swift">// 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!</code></pre>    <p>UIButton Extensions</p>    <pre>  <code class="language-swift">// 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!</code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/7e1a9643f642ed1e814bc354a1ae488e.png"></p>    <p>UIImage Extensions</p>    <pre>  <code class="language-swift">// 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!</code></pre>    <p>UIImageView Extensions</p>    <pre>  <code class="language-swift">// 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!</code></pre>    <p>UINavigationBar Extensions</p>    <pre>  <code class="language-swift">// Change navigation bar font and color  navbar.setTitleFont(UIFont, with color: UIColor.black)    // and many others!</code></pre>    <p>UINavigationController Extensions</p>    <pre>  <code class="language-swift">// Pop ViewController with completion handler.  navController.popViewController(completion: (()->Void)?)    // Push ViewController with completion handler.  navController.pushViewController(UIViewController, completion: (()->Void)?)    // and many others!</code></pre>    <p>UITableView Extensions</p>    <pre>  <code class="language-swift">// 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!</code></pre>    <p>Misc Extensions</p>    <pre>  <code class="language-swift">// 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!</code></pre>    <p> </p>    <p> </p>    <p> </p>    <p> </p>