Giter VIP home page Giter VIP logo

sweeterswift's Introduction

SweeterSwift

Swift Version Build Status License CocoaPods Compatible
Platform PRs Welcome

Extensions and syntactic sugar to enrich the Swift standard library, iOS frameworks, and SwifterSwift.

Installation

CocoaPods:

pod 'SweeterSwift'

Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/yonat/SweeterSwift", from: "1.1.4")
]

Usage

Auto Layout

Add button at the center of view:

view.addConstrainedSubview(button, constrain: .centerX, .centerY)

Put label over textField:

view.constrain(label, at: .leading, to: textField)
view.constrain(textField, at: .top, to: label, at: .bottom, diff: 8)

Add child view controller covering all but the bottom margin:

addConstrainedChild(vc, constrain: .bottomMargin, .top, .left, .right)

Bundle

App name with reasonable fallback to process name:

let appName = Bundle.main.name

App info string with name, version, and build number:

let appInfo = Bundle.main.infoString // "MyApp 1.0 #42"

Codable

Create object from a dictionary:

let decodableObject = MyDecodableClass(dictionary: aDictionary)

Export object to a dictionary:

let aDictionary = encodableObject.dictionary

DateFormatter

Create with format:

let dateFormatter = DateFormatter(format: "cccc, MMMM dd")

NotificationCenter

Post a local notification using NotificationCenter.default:

notify(notificationName, userInfo: infoDictionary)

Respond to a local notification from NotificationCenter.default:

observeNotification(notificationName, selector: #selector(someFunc))

NSAttributedString

Create from HTML:

let attributedString = NSAttributedString(htmlString: "Hello, <b>world</b>!")

Turn a substring into a link:

mutableAttributedString.link(anchorText: "Hello", url: "https://ootips.org")

NSManagedObjectContext

Dump contents to console for debugging:

managedObjectContext.printAllObjects()

Create a copy of the store for backup or for using later as initial setup:

managedObjectContext.backupStore()

String

Separate CamelCase into capitalized words:

let words = "winterIsComing".unCamelCased // "Winter Is Coming"

Change CamelCase into snake_case:

let key = "winterIsComing".camelToSnakeCased // "winter_is_coming"

Swift Standard Library

Index of current enum case in allCases:

let index = MyEnum.someCase.index

Unwrap collection, shorthand for compactMap { $0 }:

let nonOptionals = optionalsCollection.compact

Avoid retain cycles when passing a member function as an @escaping closure:

var closure = weak(self, in: MyClass.someFunction)

TimeInterval

Standard time intervals:

let myInterval: TimeInterval = .year + .month + .week + .day + .hour + .minute

UIApplication

Find the top-most view controller:

let topVC = UIApplication.topViewController()

Present modal over the top view controller:

UIApplication.present(modalVC)

UILabel / UITextView

Create a label with links by using a UITextView to auto-detect links and simulate UILabel appearance:

let hyperlinkedLabel = UITextView(simulatedLabelWithLinksInText: "More at https://ootips.org")

UIStackView

Remove an arranged subview from the view hierarchy, not just the stack arrangement:

stackView.removeArrangedSubviewCompletely(subview)

Remove all arranged subviews from the view hierarchy, not just the stack arrangement:

stackView.removeAllArrangedSubviewsCompletely()

UIView

Search the view hierarchy recursively for a subview that meets a condition:

let blueSubview = view.viewInHierarchy(where { $0.backgroundColor == .blue })

Search the view hierarchy recursively for a subview with a specific class:

let button = view.viewWithClass(UIButton.self)

Meta

@yonatsharon

https://github.com/yonat/SweeterSwift

sweeterswift's People

Contributors

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