復習OptionSet

 復習OptionSet

Fb69c72d5af289d6674d2659246a5291?s=128

Tomohiro Nishimura

December 26, 2016
Tweet

Transcript

  1. ෮श0QUJPO4FU  ؔ੢ϞόΠϧΞϓϦݚڀձ

  2. JE4JYFJHIU w ג ͸ͯͳΞϓϦέʔγϣϯΤϯδχΞ w ۙگ w י઒Ϗʔϧͱ͍͏ΠϕϯτͰ࣌ؒҿΈଓ͚ ͨΒඓਫ͕ࢭ·Βͳ͘ͳΓ·ͨ͠

  3. None
  4. 0QUJPO4FU

  5. ໨త w Ϣʔβʔ͕ૢ࡞ՄೳͳॲཧΛࢦఆ͢Δ w ྫ͑͹ w ಡΜͰ΋͍͍͚Ͳɺॻ͖ࠐΉࣄ͸Ͱ͖ͳ͍ͱ͔ w ฤू͸Ͱ͖Δ͚Ͳɺ৽ن࡞੒͸Ͱ͖ͳ͍ͱ͔

  6. &OVNFSBUJPO enum Permission { case read case create case edit

    case delete }
  7. &OVNFSBUJPO let permissions: [Permission] = [.read, .create] func read() {

    guard permissions.contains(.read) else { forbidden() return } showReadViewController() }
  8. &OVNFSBUJPO w ϦιʔεͷύʔϛογϣϯͱϢʔβʔͷݖݶ w ͜ͷϦιʔεʹରͯ͜͠ͷϢʔβʔ͕Մೳͳૢ࡞͸ ͳΜͳͷ͔ w ͞Βʹάϧʔϓຖʹݖݶ͕͋ͬͨΓ͢Δͱʜ let resourcePermissions:

    [Permission] = [.read] let userPermissions: [Permission] = [.read, .create, .edit, .delete] resourcePermissions.filter { permission in return userPermissions.contains(permission) }
  9. None
  10. #JUXJTFPQFSBUJPO // _ _ _ _ delete edit create read

    // 0 0 0 0 1 1 1 1 struct Permission { static let read: UInt8 = 0b00000001 static let create: UInt8 = 0b00000010 static let edit: UInt8 = 0b00000100 static let delete: UInt8 = 0b00001000 } 6*OUʹ໊લ͚ͭΔͱྑͦ͞͏ʜ
  11. #JUXJTFPQFSBUJPO let permissions: UInt8 = Permission.read | Permission.create func read()

    { guard (permissions & Permission.read) != 0 else { forbidden() return } showReadViewController() }
  12. #JUXJTFPQFSBUJPO w ͜ͷϦιʔεʹରͯ͜͠ͷϢʔβʔ͕Մೳͳૢ࡞͸ ͳΜͳͷ͔ let resource: UInt8 = Permission.read let

    user: UInt8 = Permission.read | Permission.create | Permission.edit | Permission.delete let currentPermission = resource & user
  13. None
  14. 0QUJPO4FU struct Permission: OptionSet { let rawValue: UInt8 static let

    read = Permission(rawValue: 1 << 0) static let create = Permission(rawValue: 1 << 1) static let edit = Permission(rawValue: 1 << 2) static let delete = Permission(rawValue: 1 << 3) }
  15. 0QUJPO4FU let permissions: Permission = [.read, .create] func read() {

    guard permissions.contains(.read) else { forbidden() return } showReadViewController() }
  16. 4FU"MHFCSB func contains(Self) func insert(Self) func update(with: Self) func remove(Self)

    func union(Self) func intersection(Self) func symmetricDifference(Self) func formUnion(Self) func formIntersection(Self) func formSymmetricDifference(Self)
  17. 4FU"MHFCSB let p: Permission = [.read, .create, .edit] let o:

    Permission = [.edit, .delete] p //=> 0111 o //=> 1100 p.union(o) //=> 1111 p.intersection(o) //=> 0100 p.symmetricDifference(o) //=> 1011
  18. 0QUJPO4FU w ͜ͷϦιʔεʹରͯ͜͠ͷϢʔβʔ͕Մೳͳૢ࡞͸ ͳΜͳͷ͔ let resource: Permission = [.read] let

    user: Permission = [.read, .create, .edit, .delete] let current: Permission = resource.intersection(user)
  19. None
  20. ΍Γ͍ͨ if case Permission.read = userPermission { print("Can read") }

  21. ΍Γ͍ͨ switch userPermission { case Permission.read: print("Can Read") fallthrough case

    Permission.create: print("Can Create") fallthrough case Permission.edit: print("Can edit") fallthrough case Permission.delete: print("Can delete") }
  22. dPQFSBUPS extension Permission { static func ~=(lhs: Permission, rhs: Permission)

    -> Bool { return rhs.contains(lhs) } }
  23. None
  24. ༇շͳ஥ؒͨͪ

  25. ༇շͳ஥ؒͨͪ w 6*$POUSPM4UBUF w OPSNBM w EJTBCMFE w FUDʜ button.setTitle("དྷ೥΋",

    for: .normal) button.setTitle("ؔϞό", for: [.highlighted, .selected]) button.setTitle("ΑΖ͘͠", for: .disabled)
  26. Α͍͓೥Λ