View Central.swift
func centralManager(central: CBCentralManager,
didDiscoverPeripheral peripheral: CBPeripheral,
advertisementData: [String : AnyObject],
RSSI: NSNumber) {
guard let packet = advertisementData[CBAdvertisementDataLocalNameKey] as? String else {
return
}
let version = Int(packet[packet.startIndex..<packet.startIndex.advancedBy(2)])
let channel = Int(packet[packet.startIndex.advancedBy(2)..<packet.startIndex.advancedBy(4)])
let cardRawValue = Int(packet[packet.startIndex.advancedBy(4)..<packet.startIndex.advancedBy(6)], radix: 16)
View Peripheral.swift
self.peripheral.startAdvertising([
CBAdvertisementDataLocalNameKey: "0001FE전수열",
CBAdvertisementDataServiceUUIDsKey: [serviceUUID],
])
View Card.swift
public enum Card: Int {
case Zero = 0
case Half = 127
case One = 1
case Two = 2
case Three = 3
case Five = 5
case Eight = 8
case Thirteen = 13
case Twenty = 20
View Central.swift
import CoreBluetooth
let serviceUUID = CBUUID(string: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
let service = CBMutableService(type: serviceUUID, primary: true)
/// 1. `CBCentralManager`를 초기화하고,
self.central = CBCentralManager(delegate: self, queue: nil)
/// 2. 사용가능한 상태가 되면 특정 UUID를 가진 서비스를 스캔합니다.
func centralManagerDidUpdateState(central: CBCentralManager) {
View Peripheral.swift
import CoreBluetooth
let serviceUUID = CBUUID(string: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
let service = CBMutableService(type: serviceUUID, primary: true)
/// 1. `CBPeripheralManager`를 초기화하고,
self.peripheral = CBPeripheralManager(delegate: self, queue: nil)
/// 2. 사용가능한 상태가 되면 특정 UUID를 가진 서비스를 추가한 뒤에
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
View ReactorKit+Plugin.swift
extension Reactor {
public func plugin<PluginReactor>(
keyPath: KeyPath<State, PluginReactor?>,
mutation mutationFactory: @escaping (PluginReactor) -> Mutation
) -> Observable<Mutation> where PluginReactor: Reactor {
return self.stateDidCreate.flatMapLatest { [weak self] () -> Observable<Mutation> in
guard let `self` = self else { return .empty() }
return self.state.flatMapLatest { state -> Observable<Mutation> in
guard let pluginReactor = state[keyPath: keyPath] else { return .empty() }
let pluginStates = Observable.merge(pluginReactor.state.skip(1))
View test_get_collection.py
# 실제 API
def get_collections(serializer):
collections = orm.foo()
paging = orm.bar()
return serializer.serialize(items=collections, paging=paging)
# 테스트
def test_serializer_get_collections():
collections = fixture_collections()
paging = fixture_paging()
View RxImmutableMappable.swift
extension ObservableType where E: Any {
func map<T: ImmutableMappable>(_ mappableType: T.Type) -> Observable<T> {
return self
.map { try Mapper<T>().map(JSONObject: $0) }
.do(onError: { error in
if error is MapError {
log.error(error)
}
})
}
View fastcampus-ios-examples.swift
//
// 배열과 클로저 활용
//
[1, 3, 6, 2, 7, 9]
.filter { $0 % 2 == 0 } // 짝수 필터링
.map { $0 * 2 } // 모든 값에 곱하기 2
.reduce(0, +) // 초깃값부터 각각의 값에 더하기 연산
View swiftkorea.swift
/*
뭔가 퍼즐같은 RxSwift 질문입니다.
서버에 3 페이지의 분량을 한꺼번에 요청하려는데,
요청은 동시에 보내고 응답은 순서대로 처리하고 싶습니다.
테스트 시나리오:
각 요청(Observable)들은 subscribe 될 때 request가 시작
각 요청의 request에는 2, 4, 2초씩 걸린다고 가정
-> 2초 후 첫 페이지가 뜨고
-> 추가로 2초 후 나머지가 전부 뜨는
상황을 원하고 있습니다.