Skip to content

Instantly share code, notes, and snippets.

View T-Pham's full-sized avatar
🖖
Hi!

Thanh Pham T-Pham

🖖
Hi!
View GitHub Profile
@T-Pham
T-Pham / UIScreen+Orientation.swift
Created June 24, 2021 02:10
Get UIInterfaceOrientation based on UIScreen's UICoordinateSpaces
extension UIScreen {
var orientation: UIInterfaceOrientation {
let convertedZeroPoint = fixedCoordinateSpace.convert(CGPoint.zero, to: coordinateSpace)
let coordinateSpaceBounds = coordinateSpace.bounds
switch convertedZeroPoint {
case .zero:
return .portrait
case CGPoint(x: coordinateSpaceBounds.maxX, y: coordinateSpaceBounds.minY):
return .landscapeLeft
case CGPoint(x: coordinateSpaceBounds.minX, y: coordinateSpaceBounds.maxY):
@T-Pham
T-Pham / gist:30e2cc17f1179251d2c4df8b86384036
Last active January 26, 2021 20:22
An ugly and quick attempt to convert SnapKit to native NSLayoutConstraint
import Foundation
let s = """
aview.snp.makeConstraints { (make) -> Void in
make.centerX.equalTo(view.snp.centerX)
make.bottom.equalTo(anotherview.snp.top).offset(-10)
make.height.equalTo(40)
make.width.equalTo(view.snp.width)
}
@T-Pham
T-Pham / MyButton.swift
Last active May 26, 2020 11:50
Custom UIControlState
class MyButton: UIButton {
var error: Bool = false {
didSet {
setNeedsLayout()
}
}
override var state: UIControlState {
get {
import Foundation
print("Type your age: ")
if let input = readLine() {
print("You typed \(input)\n")
}
print("Type your birth year: ")
@T-Pham
T-Pham / SimplifiedCode.swift
Last active May 18, 2019 08:44
Checking array type issue in Swift
import Foundation
// Alamofire
enum AlamofireReponse<T, E> {
case Success(T)
case Failure(E)
}
func responseJSON(alamofireReponse: AlamofireReponse<AnyObject, NSError> -> Void) {
alamofireReponse(.Success([["type": "not_object"], ["type": "not_object"]]))
@T-Pham
T-Pham / DynamicObject.swift
Created April 24, 2019 22:36
DynamicObject.swift
import Foundation
@dynamicMemberLookup
class DynamicObject: NSObject {
subscript<T>(dynamicMember member: String) -> T? {
get {
return objc_getAssociatedObject(self, member) as? T
}
set {
@T-Pham
T-Pham / M.swift
Created April 24, 2019 22:17
M object
@dynamicMemberLookup
@dynamicCallable
struct M {
subscript(dynamicMember member: String) -> M {
get {
return self
}
set {
}
@T-Pham
T-Pham / playground.swift
Created October 19, 2018 16:59
Swift - in-place mutation
func address(o: UnsafeRawPointer) -> Int {
return Int(bitPattern: o)
}
func addressHeap<T: AnyObject>(o: T) -> Int {
return unsafeBitCast(o, to: Int.self)
}
////////////////////////////////////////////
enum Theme {
case light
case dark
}
@T-Pham
T-Pham / test.swift
Last active September 3, 2018 07:37
url regex :(
import Foundation
let qqq = "https://yahoo.com/:;(&;&:93@;&:&&:'’'"
NSURL(string: qqq)
let tests = [
("google.com", true),
("www.google.com", true),
("http://google.com", true),
("http://www.google.com", true),