Skip to content

Instantly share code, notes, and snippets.

enum Team {
case iOS
case android
}
protocol Engineer {
var team: Team { get }
var canDevelopiOS: Bool { get }
}
protocol Swiftable {
var isKnowSwift: Bool { get }
}
struct Steven: Engineer, Swiftable {
let team: Team
let canDevelopiOS: Bool
let isKnowSwift: Bool
}
protocol Swiftable {
var isKnowSwift: Bool { get }
}
protocol Engineer {
var team: Team { get }
var canDevelopiOS: Bool { get }
}
extension Engineer {
struct Sam: Engineer {
let team: Team
}
let sam = Sam(team: .android)
print("Sam can develop iOS: \(sam.canDevelopiOS)”)
// Sam can develop iOS: false
struct Sam: Engineer {
let team: Team
let canDevelopiOS: Bool
}
let sam = Sam(team: .android, canDevelopiOS: true)
print("Sam can develop iOS: \(sam.canDevelopiOS)”)
// Sam can develop iOS: true
struct Sam: Engineer, Swiftable {
let team: Team
let isKnowSwift: Bool
}
let sam = Sam(team: .android, isKnowSwift: true)
print("Sam can develop iOS: \(sam.canDevelopiOS)")
// Sam can develop iOS: true
protocol Engineer: CustomStringConvertible {
var team: Team { get }
var canDevelopiOS: Bool { get }
}
extension CustomStringConvertible where Self: Engineer {
var description: String {
switch team {
case .android:
return "I belong to Android team."
print(steven.description)
// I belong to iOS team.
import UIKit
import SwiftyJSON
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String
let storeVersion = json["results"][0]["version"].stringValue
if appVersion == storeVersion {
// 已是最新版本
} else {
// Do something
}