Skip to content

Instantly share code, notes, and snippets.

View bob910078's full-sized avatar

BobChang bob910078

View GitHub Profile
import UIKit
func printf<T>(msg message: T, file: String = #file, method: String = #function, line: Int = #line) {
let fileName = (file as NSString).lastPathComponent
print("\(fileName) [\(line)], \(method): \(message)")
}
// 延遲指定時間 (可搭配 activity indicator 使用)
func delay(_ seconds: Double, completion: @escaping ()->()) {
let dispatchTime: DispatchTime = DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
completion()
}
}
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
import Foundation
extension UIDevice {
//MARK: Get String Value
func MBFormatter(_ bytes: Int64) -> String {
let formatter = ByteCountFormatter()
formatter.allowedUnits = ByteCountFormatter.Units.useMB
formatter.countStyle = ByteCountFormatter.CountStyle.decimal
#if swift(>=5.0)
print("Hello, Swift 5.0")
#elseif swift(>=4.2)
print("Hello, Swift 4.2")
#elseif swift(>=4.1)
print("Hello, Swift 4.1")
#elseif swift(>=4.0)
print("Hello, Swift 4.0")
#elseif swift(>=3.0)
print("Hello, Swift 3.0")
func cropToSquare(image: UIImage) -> UIImage? {
if let cgImg = image.cgImage {
let contextSize: CGSize = image.size
var posX: CGFloat = 0
var posY: CGFloat = 0
var length: CGFloat = 0
if contextSize.width > contextSize.height {
override func viewDidAppear(_ animated: Bool) {
if let img = UIImage(named: "Sonic") {
let considerationPaperSize = CGSize(width: 1800, height: 2400)
let outputImage = self.allocate(image: img, inPhotoPaper: considerationPaperSize)
Thread.updateUI(byType: .async) {
self.myImage.image = outputImage
//
// main.c
// test
//
// Created by Bob Chang on 2019/3/2.
// Copyright © 2019 Bob Chang. All rights reserved.
//
#include <stdio.h>
import UIKit
import EventKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//
// ViewController.swift
// Font
//
// Created by Bob on 2019/7/9.
// Copyright © 2019 Hamster. All rights reserved.
//
import UIKit