This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol CallBackDelegate { | |
func somethingHappened() | |
} | |
class FirstClass { | |
var delegate : CallBackDelegate? | |
func doSomething() { | |
delegate?.somethingHappened() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: A SpriteKit based Playground | |
import PlaygroundSupport | |
import SpriteKit | |
extension CGPoint { | |
static func CenterOfScene(_ scene: GameScene) -> CGPoint { | |
return CGPoint(x: scene.size.width / 2, y: scene.size.height / 2) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol SomeProtocol { | |
var required: String { get } | |
var optional: String? { get } | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension SomeProtocol { | |
var optional: String? { return nil } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ConformsWithoutOptional { | |
let required: String | |
} | |
struct ConformsWithOptional { | |
let required: String | |
let optional: String? | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Date { | |
func timeAgoSinceDate(date:NSDate, numericDates:Bool) -> String { | |
let calendar = NSCalendar.current | |
let unitFlags: Set<Calendar.Component> = [.minute, .hour, .day, .weekOfYear, .month, .year, .second] | |
let now = NSDate() | |
let earliest = now.earlierDate(date as Date) | |
let latest = (earliest == now as Date) ? date : now | |
let components = calendar.dateComponents(unitFlags, from: earliest as Date, to: latest as Date) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func estimatedFrame(forText text: String) -> CGRect { | |
// width is same as cell.texView width | |
//height is an arbitrary larg value | |
let size = CGSize(width: 200, height: 1000) | |
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) | |
let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)] | |
return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ChatController { | |
func setupKeyboardObservers() { | |
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow(withNotification:)), name: .UIKeyboardWillShow, object: nil) | |
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillHide(withNotification:)), name: .UIKeyboardDidHide, object: nil) | |
} | |
@objc func handleKeyboardWillShow(withNotification notification: Notification) { | |
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue | |
let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue | |
containerViewBottomAnchor?.constant = -(keyboardFrame?.height)! // bottomAnchor + constant | |
UIView.animate(withDuration: keyboardDuration!) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = [Int]() | |
for x in 1...100 { | |
numbers.append(x) | |
} | |
var num = [1,2,4,6,8,9,11,13,16,17,20] | |
func binarySearch(for searchValue: Int, withDatasoucre datasource: [Int]) -> Bool { | |
var firstIndex = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isValidEmail(testStr:String) -> Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" | |
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) | |
return emailTest.evaluate(with: testStr) | |
} |
OlderNewer