Posts

Showing posts with the label Swift

Swift: Understanding Mutating Functions

 classes are reference type whereas structures and enumerations are value types. The properties of value types cannot be modified within its instance methods by default. In order to modify the properties of a value type, you have to use the mutating keyword in the instance method. With this keyword, your method can then have the ability to mutate the values of the properties and write it back to the original structure when the method implementation ends. Below is a simple implementation of Stack in Swift that illustrates the use of mutating functions.     struct Stack { public private ( set ) var items = [ Int ] ( ) // Empty items array mutating func push ( _ item : Int ) { items . append ( item ) } mutating func pop ( ) - > Int ? { if ! items . isEmpty { return items . removeLast ( ) } return nil } } var stack = Stack ( ) stack . push ( 4 ) stack . push ( 78 ) stack . ...

Scroll UITextField above Keyboard in a UITableView OR UIScrollView in Swift and Objective C

In Objective C -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {     CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:_tableview];     CGPoint contentOffset = _tableview.contentOffset;         contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);         NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));        [_tableview setContentOffset:contentOffset animated:YES];        return YES; } -(BOOL)textFieldShouldEndEditing:(UITextField *)textField {     [textField resignFirstResponder];        if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])     {         CGPoint buttonPosition = [textField convertPoint:CGPointZero  ...