Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swiftでライブラリを書く際のプラクティス

 Swiftでライブラリを書く際のプラクティス

Swiftでライブラリ(人から使われるコード)を書く際のベストプラクティスを考える

Kishikawa Katsumi

February 14, 2015
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Technology

Transcript

  1. σʔλܕ • Class • Struct • Enum • Tuple •

    Function • Array • Dictionary • Set *New* • Optional
  2. public class func requestSharedWebCredential(completion: (credentials: [[String: Stri public class func

    requestSharedWebCredential(#domain: String, completion: (credential public class func requestSharedWebCredential(#domain: String, account: String, comple private class func requestSharedWebCredential(#domain: String?, account: String?, com
  3. convenience init() convenience init(service: String) convenience init(accessGroup: String) convenience init(service:

    String, accessGroup: String) convenience init(server: String, protocolType: ProtocolType) convenience init(server: String, protocolType: ProtocolType, authenticationTy convenience init(server: NSURL, protocolType: ProtocolType) convenience init(server: NSURL, protocolType: ProtocolType, authenticationTyp
  4. convenience init(service: String = "", accessGroup: String = "") convenience

    init(server: NSURL, protocolType: ProtocolType = .HTTPS, authenticationType: AuthenticationType = .HTMLForm)
  5. convenience init(service: String = default, accessGroup: String = default) convenience

    init(server: NSURL, protocolType: KeychainAccess.ProtocolType = default, authenticationType: KeychainAccess.AuthenticationType = default) ࣮ࡍͷ஋΋Θ͔Βͳ͍
  6. keychain = Keychain(server: "https://github.com", protocolType: .HTTPS) .label("github.com (kishikawakatsumi)") .comment("github access

    token") .set("01234567-89ab-...", key: "kishikawakatsumi") keychain = Keychain(server: "https://github.com", protocolType: .HTTPS) .label("github.com (Hiro Hamada)") .comment("github access token") .set("01234567-89ab-...", key: "hirohamada")
  7. // // Either.swift // swiftz // // Created by Maxwell

    Swadling on 3/06/2014. // Copyright (c) 2014 Maxwell Swadling. All rights reserved. // import class Foundation.NSError /// The Either type represents values with two possibilities: a value of type Either <A, B> is either /// Left <A> or Right <B>. /// /// The Either type is sometimes used to represent a value which is either correct or an error; by /// convention, the Left constructor is used to hold an error value and the Right constructor is /// used to hold a correct value (mnemonic: "right" also means "correct"). public enum Either<L, R> { case Left(Box<L>) case Right(Box<R>) /// Converts a Either to a Result, which is a more specialized type that /// contains an NSError or a value. public func toResult(ev : L -> NSError) -> Result<R> { return either({ e in Result.Error(ev(e)) }, { v in .Value(Box(v)) }); } /// Much like the ?? operator for Optional types, takes a value and a function, /// and if the Either is Left, returns the value, otherwise maps the function over /// the value in Right and returns that value. public func fold<B>(value : B, f : R -> B) -> B { return either({ _ in value }, { r in f(r) }); }
  8. // // Result.swift // swiftz // // Created by Maxwell

    Swadling on 9/06/2014. // Copyright (c) 2014 Maxwell Swadling. All rights reserved. // import class Foundation.NSError import typealias Foundation.NSErrorPointer /// Result is similar to an Either, except specialized to have an Error case that can /// only contain an NSError. public enum Result<V> { case Error(NSError) case Value(Box<V>) public init(_ e: NSError?, _ v: V) { if let ex = e { self = Result.Error(ex) } else { self = Result.Value(Box(v)) } } /// Converts a Result to a more general Either type. public func toEither() -> Either<NSError, V> { switch self { case let Error(e): return .Left(Box(e)) case let Value(v): return Either.Right(Box(v.value)) } }
  9. public enum FailableOf<T> { case Success(Value<T?>) case Failure(NSError) init(_ value:

    T?) { self = .Success(Value(value)) } init(_ error: NSError) { self = .Failure(error) } public var error: NSError? { switch self { case .Failure(let error): return error default: return nil } } public var value: T? { switch self { case .Success(let success): return success.value default: return nil } } }
  10. let failable = keychain.getStringOrError("kishikawakatsumi") switch failable { case .Success: println("token:

    \(failable.value)") case .Failure: println("error: \(failable.error)") }
  11. let failable = keychain.getStringOrError("kishikawakatsumi") if let error = failable.error {

    println("error: \(failable.error)") } else { println("token: \(failable.value)") }