jinios / swift-storeapp

쇼핑몰 앱 - 코드스쿼드 미션 (2018.07 - 2018.08)
0 stars 0 forks source link

[Step5] Feedback from JK #8

Open jinios opened 6 years ago

jinios commented 6 years ago

1. 객체 역할 분리

// StoreItems.swift

    func set(with category: Category) {
        URLSession.shared.dataTask(with: category.url) { [self, category] (data, response, error) in
            if let error = error {
                print("DataTask error: \(error)\n")
                return
            }
            if let response = response as? HTTPURLResponse, response.statusCode == 200, let data = data {
                let items = ItemDataParser.makeStoreItemsFromSession(category: category, data: data)
                self.setFromURL(items)
            } else {
                print("DataTask error: response \n")
                return
            }
        }.resume()
    }

    func setFromURL(_ items: [Category: Items]) {
        self.update(key: items.keys.first!, value: items.values.first!)
        let indexPaths = Array(0..<self.storeItem[items.firstKey()]!.count()).map {IndexPath(row: $0, section: items.firstKey().sectionNumber)}
        NotificationCenter.default.post(name: .sectionSetComplete,
                                        object: self,
                                        userInfo: [Keyword.sectionPath : indexPaths])
    }