Skip to content

Instantly share code, notes, and snippets.

View mosluce's full-sized avatar

mosluce mosluce

View GitHub Profile
@mosluce
mosluce / Muti2One.swift
Last active January 19, 2017 05:36
Multi ViewControllers to One
class AVC: UIViewController {
weak var delegate: AVCDelegate?
// TODO: implement
func hello() {
// 請 Main 幫忙做點事
self.delegate?.somethingFromA(self)
}
@mosluce
mosluce / Example.swift
Last active January 14, 2017 04:41
對於常用的 WebAPI 呼叫方式進行擴充 URLSession
/// 範例中使用了 SVProgressHUD, SwiftyJSON
/// 另外 UIAlertController 有 extension
func request(_ path: String, method: HttpMethod = .get, parameters: [String: Any]? = nil, localizedStatus: String? = NSLocalizedString("資料處理中", comment: "呼叫API讀取狀態文字"), _ completion: @escaping (_ json: JSON) -> Void) {
// 顯示 ActivityIndicator
if let status = localizedStatus { SVProgressHUD.show(withStatus: status) }
// 待會使用 GCD 版本所以要 Dispatch 到其他 thread
DispatchQueue.global().async {
import Foundation
import UIKit
print("==== Basic ====")
// Automatic Reference Counting
class A {
func hello() -> String {
return "world"
func requestSync(_ path: String, method: HttpMethod = .get, parameters: [String: Any]? = nil, localizedStatus: String? = nil) -> JSON? {
var out: JSON?
let semaphore = DispatchSemaphore(value: 0)
self.request(_ path, method: method, parameters: parameters, localizedStatus: localizedStatus) { (json) in
out = json
semaphore.signal()
}
// 就上面的案例重組一下
import UIKit
struct FilmFestival {
var Name: String = ""
var FormattedPrice: Int = 0
}
struct Ticketing {
var Name: String = ""
var FormattedPrice: Int = 0
@mosluce
mosluce / PhotoCropperMaskView.swift
Created December 16, 2016 09:30
照片剪裁工具
//
// CropperView.swift
// Gotyou
//
// Created by 默司 on 2016/12/15.
// Copyright © 2016年 默司. All rights reserved.
//
import UIKit
@mosluce
mosluce / NSObject.swift
Last active March 1, 2020 11:51
超惰性 extension 大全 + 默司微調
//
// NSObject.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
@mosluce
mosluce / Example.swift
Last active December 1, 2016 14:53
Protocol Oriented 練習:Deserialize JSON and XML
import Foundation
struct User {
var username: String?
var password: String?
}
extension User {
init(_ node: NodeAccessable) {
self.username = node.child("username").string
//
// Deserializable.swift
// MapperDemo
//
// Created by 默司 on 2016/12/1.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import SwiftyJSON
@mosluce
mosluce / BorderDesignable.swift
Last active April 12, 2020 11:32
Protocol Oriented 結合 UIView 及 IBDesignable+IBInspectable 練習
//
// BorderDesignable.swift
// Gotyou
//
// Created by 默司 on 2016/11/28.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit