Skip to content
Manipulate an object with a closure
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Carthage/Build/iOS
With mac
With Support mac Jan 25, 2019
WithExample.xcodeproj
WithExample Add lint May 7, 2019
src Support mac Jan 25, 2019
.swiftlint.yml
LICENSE
README.md

README.md

With 💗

mit platform Lang Carthage compatible codebeat badge

What is it:

An extension that let's you manipulate an object with a closure

How does it work:

See Example

How can I get it:

  • Carthage: github "eonist/With" "master"
  • Cocoapods: Coming soon
  • Manual installation: Open .xcodeproj

Example:

//EXAMPLE 1:
let rectangle = with(CGRect.init(x: 0, y: 0, width: 100, height: 100)) {
  $0 = $0.offsetBy(dx: 20, dy: 20)
  $0 = $0.insetBy(dx: 10, dy: 10)
}
Swift.print(rectangle)// X:30.0, y:30.0, width:80.0, height:80.0

//EXAMPLE 2:
let color:UIColor = with(.init(red: 50, green: 100, blue: 0, alpha: 0.9)) { ( col:inout UIColor) -> Void  in
  col = col.withAlphaComponent(0.2)
}
Swift.print(color.cgColor.alpha)//0.2

//EXAMPLE 3:
var size:CGSize = CGSize(width:50,height:40)
with(size) {
  $0.width = 100
  $0.height = 50
}
Swift.print(size)//100,50

//EXAMPLE 4:
func createImageView() -> UIImageView{
   return with(.init()){
      $0.image = UIImage(named:"someGraphic")
      self.addSubview($0)
   }
}
createImage()//Adds image to view

Credit:

Thanks https://github.com/sindresorhus for teaching me this JavaScript-esque super power 💪

You can’t perform that action at this time.