Skip to content

Instantly share code, notes, and snippets.

View amosavian's full-sized avatar

Amir Abbas Mousavian amosavian

View GitHub Profile
@amosavian
amosavian / typed-accessors.md
Last active November 28, 2017 18:42
An Explanation on Why We Should Have Typed Accessors for HTTP Headers in Swift-Server

Support for RFCs

In StreamingParser.writeHeader() method in two places here and here there are references to RFC5987 which allows utf-8 encoded string for header values.

There is no way to make utf8 strings RFC5987 compliant unless we parse header values into parameter pairs, checking which parameter in header value is using non-ascii character, creating title* field out of value and reencode header value. But my implementation creates RFC5987-compliant headers in first place, which I think would improve performance considerably.

Performance

Having a shared implementation for typed header values keeps room for optimizations. Any optimization in this layer will benefit all frameworks built atop of this one. We can even cache typed header values in future if necessary.

@objc dynamic func adjustViewForKeyboardNotification(_ notification: Notification) {
guard let notif = (notification.userInfo as NSDictionary?) as? [String: AnyObject] else { return }
guard let keyboardFrame = notif[UIKeyboardFrameEndUserInfoKey]?.cgRectValue, let duration = notif[UIKeyboardAnimationDurationUserInfoKey] as? Double else {
return
}
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(duration)
if let curve = notif[UIKeyboardAnimationCurveUserInfoKey] as? Int {
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: curve)!)
}
//
// ZipFileProvider
// ExtDownloader
//
// Created by Amir Abbas on 1/30/1396 AP.
// Copyright © 1396 AP Mousavian. All rights reserved.
//
import Foundation
import FilesProvider
//
// ZipFileProvider
// ExtDownloader
//
// Created by Amir Abbas on 1/30/1396 AP.
// Copyright © 1396 AP Mousavian. All rights reserved.
//
import Foundation
import FilesProvider
//
// UnformIdentifier.swift
// ExtDownloader
//
// Created by Amir Abbas on 7/6/1396 AP.
// Copyright © 1396 AP Mousavian. All rights reserved.
//
import Foundation
import MobileCoreServices
if let tabVC = self.window?.rootViewController as? UITabBarController, let navVC = tabVC.childViewControllers[1] as? UINavigationController {
tabVC.selectedIndex = 1
let vc = tabVC.storyboard!.instantiateViewController(withIdentifier: "NewViewContoller") as! NewViewContoller
vc.data = newdata
navVC.pushViewController(vc, animated: true)
}
for navsubview in (navigationController?.navigationBar.subviews ?? []) {
let classname = NSStringFromClass(type(of: navsubview))
guard classname.hasSuffix("View") && classname.hasPrefix("UINavigationItem") else { continue }
for labelView in navsubview.subviews where labelView is UILabel {
(labelView as! UILabel).textColor = .red
}
}
func validate(id: String) -> Bool {
guard id.characters.count < 11, let num = Int64(id) else { return false }
guard (num / 10) % 1000000 > 0 else { return false }
let checkNumber = Int(num % 10)
let digits = id.characters.map { Int(String($0))! }
let digitCount = digits.count
let sum = digits.dropLast().enumerated().reduce(0) { $0 + (digitCount - $1.offset) * $1.element }
let r = sum % 11
return (r < 2 && checkNumber == r) || (r >= 2 && checkNumber == (11 - r))
}
Import UIKit
let group = DispatchGroup()
let url = URL(string: "https://www.httpbin.com/bytes/1024")!
let task1 = URLSession.shared.dataTask(with: url) { (data, response, error) in
// Do Process
group.leave()
}
extension Date {
init ? (string: String, withFormat: String = "yyyy-MM-dd'T'HH:mm:ss:SSS") {
let dateFor: DateFormatter = DateFormatter()
dateFor.dateFormat = withFormat
dateFor.locale = Locale(identifier: "en_US")
if let date = dateFor.date(from: string) {
self.init(timeIntervalSince1970: date.timeIntervalSince1970)
} else {
return nil
}