Skip to content

Instantly share code, notes, and snippets.

@sisoje
sisoje / UIImage+FixOrientation.swift
Last active June 25, 2023 20:32
Fix UIImage orientation
View UIImage+FixOrientation.swift
extension UIImage {
var fixOrientationTransform: CGAffineTransform {
let angle = CGFloat((imageOrientation.rawValue & 2) >> 1 - ((imageOrientation.rawValue & 1) << 1)) * .pi / 2
let flipX = CGFloat(1 - ((imageOrientation.rawValue & 4) >> 1))
return CGAffineTransform(scaleX: flipX, y: 1).rotated(by: angle)
}
}
View DummyTypes.swift
@objcMembers
class DummyTypes: NSObject {
@NSManaged var bool: Bool
@NSManaged var decimal: Decimal
@NSManaged var int: Int
@NSManaged var double: Double
@NSManaged var url: URL
@NSManaged var uuid: UUID
@NSManaged var date: Date
@NSManaged var string: String
View IdentityKeyPath+Bad.swift
func inPredicateBad<S: Sequence>(_ values: S) -> NSComparisonPredicate {
let ex1 = NSExpression(forKeyPath: \S.Element.self)
let ex2 = NSExpression(forConstantValue: values)
return NSComparisonPredicate(leftExpression: ex1, rightExpression: ex2, modifier: .direct, type: .in)
}
func inPredicateOk<S: Sequence>(_ values: S) -> NSComparisonPredicate {
let ex1 = NSExpression.expressionForEvaluatedObject()
let ex2 = NSExpression(forConstantValue: values)
return NSComparisonPredicate(leftExpression: ex1, rightExpression: ex2, modifier: .direct, type: .in)
View Fulfiller+Example.swift
extension XCTestExpectation {
var fulfiller: DeinitBlock {
return DeinitBlock {
print("Fulfill \(self.description)")
self.fulfill()
}
}
}
class FillfilerTests: XCTestCase {
View TestFulfill.swift
import XCTest
import Foundation
class TestFulfill: XCTestCase {
class FulfillOnRelease {
let ex: XCTestExpectation
init(_ ex: XCTestExpectation) {
self.ex = ex
}
deinit {
View Property+Example.swift
let titleLabel: UILabel = {
let l = UILabel()
l.font = .systemFont(ofSize: 22)
l.textColor = .red
l.text = "WELCOME"
return l
}()
View MutationOperator.swift
infix operator ~: MultiplicationPrecedence
@discardableResult
func ~<U: AnyObject>(object: U, block: (U) -> Void) -> U {
block(object)
return object
}
let titleLabel = UILabel() ~ {
$0.font = .systemFont(ofSize: 22)
View KeyPath.swift
// compiles:
let goodKeyPath1: KeyPath<Person, String> = \Person.name
let goodKeyPath2: KeyPath<Person, Int> = \Person.age
// doesn't compile:
let badKeyPath = \Person.password
View Person.swift
struct Person {
let name: String
let age: Int
}
let persons = [
Person(name: "Pera", age: 20),
Person(name: "Mika", age: 30),
Person(name: "Laza", age: 40)
]
View NSPredicate+Examples.swift
struct Person {
let name: String
let age: Int
}
struct Payment {
let currency: String
let amount: Double
}