-
Notifications
You must be signed in to change notification settings - Fork 0
Swift and iOS
mitochondrion edited this page Jan 30, 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
}
}
#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)
}
#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)