-
Notifications
You must be signed in to change notification settings - Fork 0
Swift and iOS
mitochondrion edited this page Feb 14, 2017
·
22 revisions
#Activity Indicator
var activityIndicator = UIActivityIndicatorView()
...
func initializeActivityIndicator() {
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
activityIndicator.color = UIColor.darkGrayColor()
activityIndicator.center = self.view.center
// activityIndicator.center = CGPointMake(view.center.x, recentSearchesTableView.center.y)
activityIndicator.hidesWhenStopped = true
view.addSubview(activityIndicator)
}
#Alert
self.alert("Oops, we need to go back!") {
self.navigationController?.popViewControllerAnimated(true)
return
}
...
private func alert(message: String, completion: () -> Void = {}) {
let alertController = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
completion()
}
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
#Safe Array Subscript Operator
extension Array {
subscript (safe index: Int) -> Element? {
return indices ~= index ? self[index] : nil
}
}
extension NSObject {
var className: String {
return String(describing: type(of: self))
}
class var className: String {
return String(describing: self)
}
}
#Array remove first by value
extension Array where Element: Equatable {
mutating func remove(_ object: Element) {
if let index = index(of: object) {
remove(at: index)
}
}
}
#Make entire view resign when tapped
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.endEditing(true)
}
#Add insets to borderless UITextField
var padding: UIEdgeInsets?
override func textRect(forBounds bounds: CGRect) -> CGRect {
if let padding = self.padding {
return UIEdgeInsetsInsetRect(bounds, padding)
} else {
return super.textRect(forBounds: bounds)
}
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
if let padding = self.padding {
return UIEdgeInsetsInsetRect(bounds, padding)
} else {
return super.placeholderRect(forBounds: bounds)
}
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
if let padding = self.padding {
return UIEdgeInsetsInsetRect(bounds, padding)
} else {
return super.editingRect(forBounds: bounds)
}
}
#NSDecimal values
let nums = ["", ".", "00", "01", ".10", ".1", "1.", "1..", "1...", "1..1", ".0", "0.", "0.1", "1.0", "1e5", "1x5", "1e", "e5", "NaN"]
for num in nums {
let numericValue = NSDecimalNumber.init(string: num)
print("[\(num)] -> [\(numericValue)] \(numericValue == NSDecimalNumber.notANumber)")
}
Output:
[] -> [NaN] true
[.] -> [0] false
[00] -> [0] false
[01] -> [1] false
[.10] -> [0.1] false
[.1] -> [0.1] false
[1.] -> [1] false
[1..] -> [1] false
[1...] -> [1] false
[1..1] -> [1] false
[.0] -> [0] false
[0.] -> [0] false
[0.1] -> [0.1] false
[1.0] -> [1] false
[1e5] -> [100000] false
[1x5] -> [1] false
[1e] -> [1] false
[e5] -> [0] false
[NaN] -> [NaN] true
#UITest examples ###Async assertions using NSPredicate
let firstCell = app.cells.elementBoundByIndex(0)
let predicateExists = NSPredicate(format: "exists == 1")
expectationForPredicate(predicateExists, evaluatedWithObject: firstCell, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
let cells = app.cells
let predicateGreaterThanZero = NSPredicate(format: "count > 0")
expectationForPredicate(predicateGreaterThanZero, evaluatedWithObject: cells, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)