Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

0QUJPO4FU

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

&OVNFSBUJPO enum Permission { case read case create case edit case delete }

Slide 7

Slide 7 text

&OVNFSBUJPO let permissions: [Permission] = [.read, .create] func read() { guard permissions.contains(.read) else { forbidden() return } showReadViewController() }

Slide 8

Slide 8 text

&OVNFSBUJPO w ϦιʔεͷύʔϛογϣϯͱϢʔβʔͷݖݶ w ͜ͷϦιʔεʹରͯ͜͠ͷϢʔβʔ͕Մೳͳૢ࡞͸ ͳΜͳͷ͔ w ͞Βʹάϧʔϓຖʹݖݶ͕͋ͬͨΓ͢Δͱʜ let resourcePermissions: [Permission] = [.read] let userPermissions: [Permission] = [.read, .create, .edit, .delete] resourcePermissions.filter { permission in return userPermissions.contains(permission) }

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

#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ʹ໊લ͚ͭΔͱྑͦ͞͏ʜ

Slide 11

Slide 11 text

#JUXJTFPQFSBUJPO let permissions: UInt8 = Permission.read | Permission.create func read() { guard (permissions & Permission.read) != 0 else { forbidden() return } showReadViewController() }

Slide 12

Slide 12 text

#JUXJTFPQFSBUJPO w ͜ͷϦιʔεʹରͯ͜͠ͷϢʔβʔ͕Մೳͳૢ࡞͸ ͳΜͳͷ͔ let resource: UInt8 = Permission.read let user: UInt8 = Permission.read | Permission.create | Permission.edit | Permission.delete let currentPermission = resource & user

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

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) }

Slide 15

Slide 15 text

0QUJPO4FU let permissions: Permission = [.read, .create] func read() { guard permissions.contains(.read) else { forbidden() return } showReadViewController() }

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

0QUJPO4FU w ͜ͷϦιʔεʹରͯ͜͠ͷϢʔβʔ͕Մೳͳૢ࡞͸ ͳΜͳͷ͔ let resource: Permission = [.read] let user: Permission = [.read, .create, .edit, .delete] let current: Permission = resource.intersection(user)

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

΍Γ͍ͨ if case Permission.read = userPermission { print("Can read") }

Slide 21

Slide 21 text

΍Γ͍ͨ 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") }

Slide 22

Slide 22 text

dPQFSBUPS extension Permission { static func ~=(lhs: Permission, rhs: Permission) -> Bool { return rhs.contains(lhs) } }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

༇շͳ஥ؒͨͪ

Slide 25

Slide 25 text

༇շͳ஥ؒͨͪ w 6*$POUSPM4UBUF w OPSNBM w EJTBCMFE w FUDʜ button.setTitle("དྷ೥΋", for: .normal) button.setTitle("ؔϞό", for: [.highlighted, .selected]) button.setTitle("ΑΖ͘͠", for: .disabled)

Slide 26

Slide 26 text

Α͍͓೥Λ