9 Notes In April 2020
1. Microseconds to Time Interval
timestamp :
2. Check ZIP Code
8. Extension TableView Header Footer
9. Extension UiView + Blur
timestamp :
- E15 is microseconds, ex (1.589327675E15)
- E12 is milliseconds, ex (1.589327675E12)
// Check correct in here https://www.epochconverter.com/
// TIMESTAMP microseconds
let str = "1.589327675E15"
let dicStr: [String: Any] = ["date": str]
if let strDou = dicStr["date"] as? String {
if let douStr2 = Double(strDou) {
let douStr1 = TimeInterval(douStr2 / 1000.0)
print(str)
print(douStr2, "errr")
print(douStr1)
}
}
if let douStr = Double(str) {
let douStr1 = TimeInterval(douStr / 1000.0)
print(str)
print(douStr, "wwqqq")
print(douStr1)
}
}
2. Check ZIP Code
func checkValidZipCode() -> Bool { var returnValue = true let regex = "^[0-9]{5}" do { let regex = try NSRegularExpression(pattern: regex)
let nsString = self as NSString let results = regex.matches(in: self, range: NSRange(location: 0, length: nsString.length)) if results.isEmpty { returnValue = false } } catch let error as NSError { print("invalid regex: \(error.localizedDescription)") returnValue = false } return returnValue }
3. viewModel looping
func listArr(model: ResponseModel) -> [models] { var listItems: [models] = [] model.detail.forEach { detail in lisItem.appand(detail) } return listItems } // call in viewController print(viewModel.listArr(model: response))
4. Extension + UIView
extension UIView { func hideTheKeyboard() { let viewTapped: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIView.dismissKeyboard)) viewTapped.cancelsTouchesInView = true self.addGestureRecognizer(viewTapped) } @objc func dismissKeyboard() { self.endEditing(true) } }
5. Extension + UITableview
Make a simple call table view
import UIKit extension UITableView { func register<T: UITableViewCell>(cellType: T.Type, bundle: Bundle? = nil) { let className = String(describing: cellType) let nib = UINib(nibName: className, bundle: bundle) register(nib, forCellReuseIdentifier: className) } func register<T: UITableViewCell>(cellTypes: [T.Type], bundle: Bundle? = nil) { cellTypes.forEach { register(cellType: $0, bundle: bundle) } } func dequeueReusableCell<T: UITableViewCell>(with type: T.Type, for indexPath: IndexPath) -> T { let className = String(describing: type) return self.dequeueReusableCell(withIdentifier: className, for: indexPath) as! T } }
6. Simple Player For Youtube WebView
import UIKit
import WebKit
class PlayerViewController: UIViewController {
@IBOutlet var webView: WKWebView!
var urlString: String?
override func viewDidLoad() {
super.viewDidLoad()
if let url = urlString {
loadYoutube(videoID: url)
}
}
func loadYoutube(videoID: String) {
guard let youtubeURL = URL(string: videoID) else { return }
let urlReq = URLRequest(url: youtubeURL)
webView.load(urlReq)
}
}
7. Extension Class
public extension NSObject {
class var className: String {
return NSStringFromClass(self).components(separatedBy: ".").last! as String
}
}
8. Extension TableView Header Footer
extension UITableView { func registerHeaderFooter<T: UITableViewHeaderFooterView>(_: T.Type) { if let nib = T.nib { self.register(nib, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) } else { self.register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) } } }
9. Extension UiView + Blur
extension UIView { func addBlurEffect() { let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = self.bounds blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] self.addSubview(blurEffectView) } }
9 Notes In April 2020
Reviewed by Luffyselah
on
May 12, 2020
Rating: 5