Skip to content

Instantly share code, notes, and snippets.

@NickHung1982
NickHung1982 / AppDelegate.swift
Last active May 11, 2020 23:47
AppDelegate.swift
//
// AppDelegate.swift
// CryptoCurrencyPayment
//
// Created by Nick on 4/16/20.
// Copyright © 2020 Nick. All rights reserved.
//
import UIKit
import Firebase
@NickHung1982
NickHung1982 / solve1.swift
Created February 1, 2019 15:14
ConvertStringToArray
func splitCapitalString(_ textString:String) -> [String] {
var newStringArray = [String]()
for char in textString {
if String(char) == String(char).uppercased() {
newStringArray.append(" ")
}
newStringArray.append(String(char))
}
// func startLoad() {
// let url = URL(string: "https://lemulotdotorg.files.wordpress.com/2016/10/dsc00737.jpg")!
// //init data
// receivedData = Data()
// let task = session.dataTask(with: url)
// task.resume()
//
// }
func startLoad(){
//
// HTTPNetworking.swift
// urlSessionDemo
//
// Created by Nick on 12/2/18.
// Copyright © 2018 NickOwn. All rights reserved.
//
import Foundation
import UIKit
//MARK: - URLSessionDataDelegate
extension ViewController: URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
completionHandler(.cancel)
return
}
//Session with configuration
private lazy var session: URLSession = {
let config = URLSessionConfiguration.default
config.waitsForConnectivity = true
return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()
@IBAction func action_loadImage(_ sender: UIButton) {
//disable button in case user click twice
sender.isEnabled = false
//start loading image
startLoad()
}
func startLoad() {
let url = URL(string: "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")!
@NickHung1982
NickHung1982 / newPartOfCell.swift
Created November 4, 2017 21:32
update with anthor
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//add UIImageView
customImageView = UIImageView()
customImageView.translatesAutoresizingMaskIntoConstraints = false
customImageView.backgroundColor = UIColor.red
self.addSubview(customImageView)
@NickHung1982
NickHung1982 / autolayoutCustomCell.swift
Created November 4, 2017 21:24
update with use anchor
//setup centerY with cell
//let labelConstraintCenterY = NSLayoutConstraint(item: customLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
//setup uilabel's height is equal 21
//let labelConstraintHeight = NSLayoutConstraint(item: customLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 21)
//self.addConstraints([labelConstraintCenterY])
//Update use layout Anchor
customLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
customLabel.heightAnchor.constraint(equalToConstant: 21).isActive = true
@NickHung1982
NickHung1982 / cacheImg.swift
Created November 3, 2017 23:49
extension for cache download image
extension UIImageView {
func loadImageUsingCache(withUrl urlString : String) {
let url = URL(string: urlString)
self.image = nil
// check cached image
if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
self.image = cachedImage
return
}