Skip to content

Instantly share code, notes, and snippets.

View DanielNo's full-sized avatar

Daniel No DanielNo

  • New York
View GitHub Profile
@DanielNo
DanielNo / Properties.swift
Last active April 10, 2019 13:41
Swift Properties
@objc var closureInitialization : UIButton = {
// Cannot use self in closure initialization
let btn = UIButton()
return btn
}()
// Lazy property closure init
let textColor : UIColor
@objc lazy var errorMsgLabel : UILabel = {
@DanielNo
DanielNo / iOS Simulator - file directory
Created January 16, 2019 21:08
iOS Simulator - file directory
1. Set and hit a breakpoint in the app, and write the following in the Xcode Console (next to the Variables View):
2. po NSHomeDirectory()
3. open terminal and change directory
4. run command in terminal
open .
@DanielNo
DanielNo / Objective-C+Swift.m
Last active December 2, 2019 15:46
Import Statements for Swift & Objective-C interoperability
/*
Using Swift code inside an Objective-C file
*/
// In a project :
// Use product module name found in build settings
#import "ProductModuleName-Swift.h"
// Within a Framework
#import <FrameworkName/FrameworkName-Swift.h>
@DanielNo
DanielNo / DemoUITests.swift
Last active August 14, 2018 14:42
XCUITestSyntax
func testNextVC() {
// Can query elements by accessibility identifier or accessibility label
// Use Accessibility Identifier instead of accessibility label for ui tests!
let button = app.buttons["btnAccessibiityLabel"]
XCTAssertTrue(button.exists)
button.tap()
// Find ui element by accessibility identifer
let vcLabel = app.staticTexts["labelAccessibilityIdentifier"]
@DanielNo
DanielNo / RoundedCorner.swift
Created July 31, 2018 14:53
Smoother view corners
// In a view subclass's layoutSubviews()
let roundPAth = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.height / 2)
let maskLayer = CAShapeLayer()
maskLayer.path = roundPath.cgPath
layer.mask = maskLayer
@DanielNo
DanielNo / UIStackview+StringLabel
Created June 12, 2018 17:13
UIStackview Extensions
extension UIStackView {
convenience init(labelTitles: [String]) {
self.init()
for title in labelTitles {
let label = UILabel()
label.text = title
addArrangedSubview(label)
}
import UIKit
extension UIScrollView{
func currentPage() -> Int{
let page = Int((self.contentOffset.x / self.frame.size.width).rounded())
print("page num : \(page)")
return page
}
}
@DanielNo
DanielNo / String+CharAtIndex
Last active June 12, 2018 17:15
String subscripting extensions
extension String{
func charAtIndex(index : Int) -> Character{
let i = self.index (self.startIndex, offsetBy: index)
return self[i]
}
func stringAtIndex(index : Int) -> String{
let i = self.index (self.startIndex, offsetBy: index)
return String(self[i])
}
@DanielNo
DanielNo / GenericQueue.swift
Created December 5, 2017 20:20
Queue implementation with generic elements in swift 4
struct GenericQueue<Element>{
fileprivate var elements : [Element] = []
mutating func enqueue(newElement : Element){
elements.append(newElement)
}
mutating func dequeue(){
guard !elements.isEmpty else {
print("queue is empty")
@DanielNo
DanielNo / GenericStack.swift
Created December 5, 2017 20:19
Stack implementation with generic elements in swift 4
struct GenericStack<Element>{
fileprivate var elements : [Element] = []
mutating func pop(){
guard !elements.isEmpty else{
return
}
elements.removeLast(1)
}