From d01672caf5a8560aae0ea9f5e69b4badf067b295 Mon Sep 17 00:00:00 2001 From: dddowon Date: Tue, 8 Nov 2022 15:41:06 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20PR=20=EC=88=98=EC=A0=95=20=EC=A7=84?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JSONParsingTests/JSONParsingTests.swift | 21 ++++++------- .../OpenMarket.xcodeproj/project.pbxproj | 12 +++++--- .../Controller/ViewController.swift | 19 ------------ .../OpenMarket/Error/JSONParsingError.swift | 19 ++++++++++++ .../OpenMarket/Error/NetWorkError.swift | 12 ++++---- .../OpenMarket/NetWork/JsonParser.swift | 24 --------------- OpenMarket/OpenMarket/NetWork/NetWork.swift | 24 ++------------- .../OpenMarket/Protocol/JSONDecodable.swift | 30 +++++++++++++++++++ .../OpenMarket/Protocol/OpenMarketURL.swift | 10 +++---- 9 files changed, 80 insertions(+), 91 deletions(-) create mode 100644 OpenMarket/OpenMarket/Error/JSONParsingError.swift delete mode 100644 OpenMarket/OpenMarket/NetWork/JsonParser.swift create mode 100644 OpenMarket/OpenMarket/Protocol/JSONDecodable.swift diff --git a/OpenMarket/JSONParsingTests/JSONParsingTests.swift b/OpenMarket/JSONParsingTests/JSONParsingTests.swift index 0223c7a..d9addc7 100644 --- a/OpenMarket/JSONParsingTests/JSONParsingTests.swift +++ b/OpenMarket/JSONParsingTests/JSONParsingTests.swift @@ -9,7 +9,8 @@ import XCTest @testable import OpenMarket final class JSONParsingTests: XCTestCase { - let jsonParser = JsonParser() + var product: Product? + var jsonParser: JSONDecodable? override func setUpWithError() throws { try super.setUpWithError() @@ -18,19 +19,15 @@ final class JSONParsingTests: XCTestCase { override func tearDownWithError() throws { try super.tearDownWithError() } - - func test_파싱_되는지_확인() { - let product = jsonParser.jsonParsingData() - - print(product) - } - func test_파싱한데이터_pageNo와_1이랑_비교() { - let product = jsonParser.jsonParsingData() - - let pageNo = product?.pageNo + func test_파싱한데이터의_pageNo는_1이다() { + let fileName = "products" + let pageNumber = 1 + + let data = jsonParser?.mockParsingData(name: fileName, data: product) - XCTAssertEqual(1, pageNo) + print(product?.pageNo) + } } diff --git a/OpenMarket/OpenMarket.xcodeproj/project.pbxproj b/OpenMarket/OpenMarket.xcodeproj/project.pbxproj index be33246..bbdb11b 100644 --- a/OpenMarket/OpenMarket.xcodeproj/project.pbxproj +++ b/OpenMarket/OpenMarket.xcodeproj/project.pbxproj @@ -13,11 +13,12 @@ 325B7EBC29123E5000CEAF98 /* DetailPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B7EBB29123E5000CEAF98 /* DetailPage.swift */; }; 325B7EBE2913630800CEAF98 /* NetWorkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B7EBD2913630800CEAF98 /* NetWorkError.swift */; }; 32687C542907B36E005799CA /* Product.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32687C532907B36E005799CA /* Product.swift */; }; - AA0CE0252908BD8E00A41C87 /* JsonParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0242908BD8E00A41C87 /* JsonParser.swift */; }; + AA0CE0252908BD8E00A41C87 /* JSONDecodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */; }; AA0CE0282908CAEE00A41C87 /* ListPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0272908CAEE00A41C87 /* ListPage.swift */; }; AA0CE02A2908CB2100A41C87 /* Images.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0292908CB2100A41C87 /* Images.swift */; }; AA0CE02C2908CB2F00A41C87 /* Vendors.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE02B2908CB2F00A41C87 /* Vendors.swift */; }; AA0CE0342908FE0700A41C87 /* JSONParsingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0CE0332908FE0700A41C87 /* JSONParsingTests.swift */; }; + AA3D8160291A1B1600276292 /* JSONParsingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA3D815F291A1B1600276292 /* JSONParsingError.swift */; }; C70FB0FB25BEF61C00C9924E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C70FB0FA25BEF61C00C9924E /* AppDelegate.swift */; }; C70FB0FD25BEF61C00C9924E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C70FB0FC25BEF61C00C9924E /* SceneDelegate.swift */; }; C70FB0FF25BEF61C00C9924E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C70FB0FE25BEF61C00C9924E /* ViewController.swift */; }; @@ -51,12 +52,13 @@ 325B7EBB29123E5000CEAF98 /* DetailPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailPage.swift; sourceTree = ""; }; 325B7EBD2913630800CEAF98 /* NetWorkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetWorkError.swift; sourceTree = ""; }; 32687C532907B36E005799CA /* Product.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Product.swift; sourceTree = ""; }; - AA0CE0242908BD8E00A41C87 /* JsonParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonParser.swift; sourceTree = ""; }; + AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = ""; }; AA0CE0272908CAEE00A41C87 /* ListPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListPage.swift; sourceTree = ""; }; AA0CE0292908CB2100A41C87 /* Images.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Images.swift; sourceTree = ""; }; AA0CE02B2908CB2F00A41C87 /* Vendors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vendors.swift; sourceTree = ""; }; AA0CE0312908FE0700A41C87 /* JSONParsingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JSONParsingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; AA0CE0332908FE0700A41C87 /* JSONParsingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONParsingTests.swift; sourceTree = ""; }; + AA3D815F291A1B1600276292 /* JSONParsingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONParsingError.swift; sourceTree = ""; }; C70FB0F725BEF61C00C9924E /* OpenMarket.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OpenMarket.app; sourceTree = BUILT_PRODUCTS_DIR; }; C70FB0FA25BEF61C00C9924E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; C70FB0FC25BEF61C00C9924E /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -97,6 +99,7 @@ children = ( 325B2A682914CF48009354D3 /* OpenMarketAPI.swift */, 325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */, + AA0CE0242908BD8E00A41C87 /* JSONDecodable.swift */, ); path = Protocol; sourceTree = ""; @@ -105,7 +108,6 @@ isa = PBXGroup; children = ( 3258D0DA290F6E3900C25807 /* NetWork.swift */, - AA0CE0242908BD8E00A41C87 /* JsonParser.swift */, ); path = NetWork; sourceTree = ""; @@ -114,6 +116,7 @@ isa = PBXGroup; children = ( 325B7EBD2913630800CEAF98 /* NetWorkError.swift */, + AA3D815F291A1B1600276292 /* JSONParsingError.swift */, ); path = Error; sourceTree = ""; @@ -343,8 +346,9 @@ C70FB0FF25BEF61C00C9924E /* ViewController.swift in Sources */, 325B2A6B2914CF50009354D3 /* OpenMarketURL.swift in Sources */, 32687C542907B36E005799CA /* Product.swift in Sources */, + AA3D8160291A1B1600276292 /* JSONParsingError.swift in Sources */, 325B7EBE2913630800CEAF98 /* NetWorkError.swift in Sources */, - AA0CE0252908BD8E00A41C87 /* JsonParser.swift in Sources */, + AA0CE0252908BD8E00A41C87 /* JSONDecodable.swift in Sources */, C70FB0FB25BEF61C00C9924E /* AppDelegate.swift in Sources */, 3258D0DB290F6E3900C25807 /* NetWork.swift in Sources */, 325B2A692914CF48009354D3 /* OpenMarketAPI.swift in Sources */, diff --git a/OpenMarket/OpenMarket/Controller/ViewController.swift b/OpenMarket/OpenMarket/Controller/ViewController.swift index 0ff5332..377d726 100644 --- a/OpenMarket/OpenMarket/Controller/ViewController.swift +++ b/OpenMarket/OpenMarket/Controller/ViewController.swift @@ -8,27 +8,8 @@ import UIKit import Foundation class ViewController: UIViewController { - let network = NetWork() override func viewDidLoad() { super.viewDidLoad() - network.getDetailPageData { (result: Result) in - switch result { - case .success(let data): - print(data.id) - case .failure(let failure): - print(failure) - } - } - - network.getProductData { (result: Result) in - switch result { - case .success(let data): - print(data.pageNo) - case .failure(let failure): - print(failure) - } - } - } } diff --git a/OpenMarket/OpenMarket/Error/JSONParsingError.swift b/OpenMarket/OpenMarket/Error/JSONParsingError.swift new file mode 100644 index 0000000..77cc05d --- /dev/null +++ b/OpenMarket/OpenMarket/Error/JSONParsingError.swift @@ -0,0 +1,19 @@ +// +// JSONParsingError.swift +// OpenMarket +// +// Created by 박도원 on 2022/11/08. +// + +import Foundation + +enum JSONParsingError: Error, LocalizedError { + case jsonDecodingError + + var errorDescription: String { + switch self { + case .jsonDecodingError: + return "json 디코딩 오류" + } + } +} diff --git a/OpenMarket/OpenMarket/Error/NetWorkError.swift b/OpenMarket/OpenMarket/Error/NetWorkError.swift index 337aeab..fa6f08a 100644 --- a/OpenMarket/OpenMarket/Error/NetWorkError.swift +++ b/OpenMarket/OpenMarket/Error/NetWorkError.swift @@ -8,18 +8,18 @@ import Foundation enum NetWorkError: Error, LocalizedError { - case urlError, unknownError, statusError, decodingError + case urlError, unknownError, statusError, networkDecodingError var errorDescription: String { switch self { case .urlError: - return "URL을 불러오지 못했습니다." + return "url 오류" case .unknownError: - return "알 수 없는 에러입니다." + return "unknown" case .statusError: - return "status코드가 200에서 299사이가 아닙니다." - case .decodingError: - return "디코딩이 수행되지 않았습니다." + return "status코드 오류" + case .networkDecodingError: + return "네트워크 디코딩 오류" } } } diff --git a/OpenMarket/OpenMarket/NetWork/JsonParser.swift b/OpenMarket/OpenMarket/NetWork/JsonParser.swift deleted file mode 100644 index 3b17dee..0000000 --- a/OpenMarket/OpenMarket/NetWork/JsonParser.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// JsonPaser.swift -// OpenMarket -// -// Created by 박도원 on 2022/10/26. -// - -import UIKit - -class JsonParser { - enum FileName: String { - case fileName = "products" - } - - func mockParsingData() -> ListPage? { - guard let dataAsset = NSDataAsset(name: FileName.fileName.rawValue) else { - return nil - } - let jsonDecoder = JSONDecoder() - let product = try? jsonDecoder.decode(ListPage.self, from: dataAsset.data) - - return product - } -} diff --git a/OpenMarket/OpenMarket/NetWork/NetWork.swift b/OpenMarket/OpenMarket/NetWork/NetWork.swift index 3054f6c..f421e1b 100644 --- a/OpenMarket/OpenMarket/NetWork/NetWork.swift +++ b/OpenMarket/OpenMarket/NetWork/NetWork.swift @@ -10,34 +10,16 @@ import UIKit class NetWork: OpenMarketAPI { func getProductData(completion: @escaping (Result) -> Void) { - let url = host + listPath + queryParameter + let url = OpenMarketURL.host + OpenMarketURL.listPath + OpenMarketURL.queryParameter self.getOpenMarketData(url: url, completion: completion) } func getDetailPageData(completion: @escaping (Result) -> Void) { - let url = host + detailPath + let url = OpenMarketURL.host + OpenMarketURL.detailPath self.getOpenMarketData(url: url, completion: completion) } } -extension NetWork: OpenMarketURL { - var host: String { - return "https://openmarket.yagom-academy.kr/" - } - - var listPath: String { - return "api/products" - } - - var detailPath: String { - return "api/products/32" - } - - var queryParameter: String { - return "?page_no=1&items_per_page=100" - } -} - extension NetWork { func getOpenMarketData(url: String, completion: @escaping (Result) -> Void ) { guard let url = URL(string: url) else { @@ -67,7 +49,7 @@ extension NetWork { let receivedData = try JSONDecoder().decode(T.self, from: data) completion(.success(receivedData)) } catch { - completion(.failure(NetWorkError.decodingError)) + completion(.failure(NetWorkError.networkDecodingError)) } } } diff --git a/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift new file mode 100644 index 0000000..5840f5c --- /dev/null +++ b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift @@ -0,0 +1,30 @@ +// +// JsonPaser.swift +// OpenMarket +// +// Created by 박도원 on 2022/10/26. +// + +import UIKit + +protocol JSONDecodable { + +} + +extension JSONDecodable { + func mockParsingData(name: String, data: T) -> T? { + print("여기") + guard let dataAsset = NSDataAsset(name: name) else { + print("nilnilnil") + return nil + } + print("여기1") + let jsonDecoder = JSONDecoder() + guard let data = try? jsonDecoder.decode(T.self, from: dataAsset.data) else { + print("ninlninljlnio") + return nil + } + print("여기2") + return data + } +} diff --git a/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift b/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift index 25a4c54..44bab71 100644 --- a/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift +++ b/OpenMarket/OpenMarket/Protocol/OpenMarketURL.swift @@ -7,9 +7,9 @@ import Foundation -protocol OpenMarketURL { - var host: String { get } - var listPath: String { get } - var detailPath: String { get } - var queryParameter: String { get } +enum OpenMarketURL { + static let host = "https://openmarket.yagom-academy.kr/" + static let listPath = "api/products" + static let detailPath = "api/products/32" + static let queryParameter = "?page_no=1&items_per_page=100" } From 64a12d03b750eab02907c89ca73e5d26ab83f1ac Mon Sep 17 00:00:00 2001 From: eunho8713 Date: Wed, 9 Nov 2022 10:53:27 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20PR=EB=B0=98=EC=98=81=20->=20JSONProt?= =?UTF-8?q?ocol=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95,=20JSONParser?= =?UTF-8?q?=ED=83=80=EC=9E=85=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OpenMarket.xcodeproj/project.pbxproj | 4 +++ .../Controller/ViewController.swift | 3 ++- .../OpenMarket/NetWork/JSONParser.swift | 18 ++++++++++++++ .../OpenMarket/Protocol/JSONDecodable.swift | 23 +++++++----------- .../OpenMarket/View/Assets.xcassets/.DS_Store | Bin 6148 -> 6148 bytes 5 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 OpenMarket/OpenMarket/NetWork/JSONParser.swift diff --git a/OpenMarket/OpenMarket.xcodeproj/project.pbxproj b/OpenMarket/OpenMarket.xcodeproj/project.pbxproj index bbdb11b..0a6c2a5 100644 --- a/OpenMarket/OpenMarket.xcodeproj/project.pbxproj +++ b/OpenMarket/OpenMarket.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 3258D0DB290F6E3900C25807 /* NetWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3258D0DA290F6E3900C25807 /* NetWork.swift */; }; 325B2A692914CF48009354D3 /* OpenMarketAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B2A682914CF48009354D3 /* OpenMarketAPI.swift */; }; 325B2A6B2914CF50009354D3 /* OpenMarketURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */; }; + 325B2A7B291B3EE3009354D3 /* JSONParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B2A7A291B3EE3009354D3 /* JSONParser.swift */; }; 325B7EBC29123E5000CEAF98 /* DetailPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B7EBB29123E5000CEAF98 /* DetailPage.swift */; }; 325B7EBE2913630800CEAF98 /* NetWorkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 325B7EBD2913630800CEAF98 /* NetWorkError.swift */; }; 32687C542907B36E005799CA /* Product.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32687C532907B36E005799CA /* Product.swift */; }; @@ -49,6 +50,7 @@ 325B2A682914CF48009354D3 /* OpenMarketAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenMarketAPI.swift; sourceTree = ""; }; 325B2A6A2914CF50009354D3 /* OpenMarketURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenMarketURL.swift; sourceTree = ""; }; 325B2A702914D49A009354D3 /* OpenMarketNetWorkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OpenMarketNetWorkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 325B2A7A291B3EE3009354D3 /* JSONParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONParser.swift; sourceTree = ""; }; 325B7EBB29123E5000CEAF98 /* DetailPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailPage.swift; sourceTree = ""; }; 325B7EBD2913630800CEAF98 /* NetWorkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetWorkError.swift; sourceTree = ""; }; 32687C532907B36E005799CA /* Product.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Product.swift; sourceTree = ""; }; @@ -108,6 +110,7 @@ isa = PBXGroup; children = ( 3258D0DA290F6E3900C25807 /* NetWork.swift */, + 325B2A7A291B3EE3009354D3 /* JSONParser.swift */, ); path = NetWork; sourceTree = ""; @@ -350,6 +353,7 @@ 325B7EBE2913630800CEAF98 /* NetWorkError.swift in Sources */, AA0CE0252908BD8E00A41C87 /* JSONDecodable.swift in Sources */, C70FB0FB25BEF61C00C9924E /* AppDelegate.swift in Sources */, + 325B2A7B291B3EE3009354D3 /* JSONParser.swift in Sources */, 3258D0DB290F6E3900C25807 /* NetWork.swift in Sources */, 325B2A692914CF48009354D3 /* OpenMarketAPI.swift in Sources */, C70FB0FD25BEF61C00C9924E /* SceneDelegate.swift in Sources */, diff --git a/OpenMarket/OpenMarket/Controller/ViewController.swift b/OpenMarket/OpenMarket/Controller/ViewController.swift index 377d726..d1d62c1 100644 --- a/OpenMarket/OpenMarket/Controller/ViewController.swift +++ b/OpenMarket/OpenMarket/Controller/ViewController.swift @@ -1,6 +1,6 @@ // // OpenMarket - ViewController.swift -// Created by yagom. +// Created by yagom. // Copyright © yagom. All rights reserved. // @@ -11,5 +11,6 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + } } diff --git a/OpenMarket/OpenMarket/NetWork/JSONParser.swift b/OpenMarket/OpenMarket/NetWork/JSONParser.swift new file mode 100644 index 0000000..730579d --- /dev/null +++ b/OpenMarket/OpenMarket/NetWork/JSONParser.swift @@ -0,0 +1,18 @@ +// +// JSONParser.swift +// OpenMarket +// +// Created by 이은호 on 2022/11/09. +// + +import Foundation + +struct JSONParser: JSONDecodable { + var product: Product? + + func getProductData() throws -> Product? { + let fileName = "products" + guard let mock = try? mockParsingData2(name: fileName, data: product) else {throw NetWorkError.unknownError} + return mock + } +} diff --git a/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift index 5840f5c..967660a 100644 --- a/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift +++ b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift @@ -12,19 +12,14 @@ protocol JSONDecodable { } extension JSONDecodable { - func mockParsingData(name: String, data: T) -> T? { - print("여기") - guard let dataAsset = NSDataAsset(name: name) else { - print("nilnilnil") - return nil + func mockParsingData2(name: String, data: T) throws -> T? { + guard let dataAsset = NSDataAsset(name: name) else { + throw JSONParsingError.jsonDecodingError + } + let jsonDecoder = JSONDecoder() + guard let data = try? jsonDecoder.decode(T.self, from: dataAsset.data) else { + throw JSONParsingError.jsonDecodingError + } + return data } - print("여기1") - let jsonDecoder = JSONDecoder() - guard let data = try? jsonDecoder.decode(T.self, from: dataAsset.data) else { - print("ninlninljlnio") - return nil - } - print("여기2") - return data - } } diff --git a/OpenMarket/OpenMarket/View/Assets.xcassets/.DS_Store b/OpenMarket/OpenMarket/View/Assets.xcassets/.DS_Store index 5dc4ad4d469303487ff15ebd98bc3380651b62b8..8706a3a56a9cf9c29d424576e7f5b80f003026e3 100644 GIT binary patch delta 448 zcmZoMXfc=|#>B!ku~2NHo+2aX#(>?7ivyUM7&$ldFzGXL@-X-^75EnC~GL$5h7Z)Vu_{re$t*50Fu2CZ#LU9V z#?Hac!OamHoRME1T#{H)TI`fq6b<5q$kKs7PpnRzMsa@6C1=M3l12_N3}xs6hivS|LV3o`>>T_Y!1&qh$o!poGQWr;2hcVq Nps5U-BSh9P0{~R#a76$B delta 67 zcmZoMXfc=|#>B)qu~2NHo+2a5#(>?7j4YdZSo9e;i*X3DOl;V_nVo~51E^%PAjfy+ V$^0UY91K9f$iTp|IYML&GXO0{4*mcD From b04433cb02f6accef5e570fcb395f9fb6c2f8ae9 Mon Sep 17 00:00:00 2001 From: dddowon Date: Wed, 9 Nov 2022 11:07:21 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20PR=20=EC=88=98=EC=A0=95=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JSONParsingTests/JSONParsingTests.swift | 10 +++------ .../Controller/ViewController.swift | 5 +++-- .../OpenMarket/NetWork/JSONParser.swift | 6 ++--- .../OpenMarket/Protocol/JSONDecodable.swift | 22 +++++++++---------- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/OpenMarket/JSONParsingTests/JSONParsingTests.swift b/OpenMarket/JSONParsingTests/JSONParsingTests.swift index d9addc7..e89ea1d 100644 --- a/OpenMarket/JSONParsingTests/JSONParsingTests.swift +++ b/OpenMarket/JSONParsingTests/JSONParsingTests.swift @@ -9,8 +9,6 @@ import XCTest @testable import OpenMarket final class JSONParsingTests: XCTestCase { - var product: Product? - var jsonParser: JSONDecodable? override func setUpWithError() throws { try super.setUpWithError() @@ -21,13 +19,11 @@ final class JSONParsingTests: XCTestCase { } func test_파싱한데이터의_pageNo는_1이다() { - let fileName = "products" let pageNumber = 1 - - let data = jsonParser?.mockParsingData(name: fileName, data: product) - print(product?.pageNo) - + let product = try? JSONParser().getMockData() + + XCTAssertEqual(pageNumber, product?.pageNo) } } diff --git a/OpenMarket/OpenMarket/Controller/ViewController.swift b/OpenMarket/OpenMarket/Controller/ViewController.swift index d1d62c1..b4c451e 100644 --- a/OpenMarket/OpenMarket/Controller/ViewController.swift +++ b/OpenMarket/OpenMarket/Controller/ViewController.swift @@ -8,9 +8,10 @@ import UIKit import Foundation class ViewController: UIViewController { - + let jsonParser = JSONParser() override func viewDidLoad() { super.viewDidLoad() - + let a = try? jsonParser.getMockData() + print(a?.pageNo) } } diff --git a/OpenMarket/OpenMarket/NetWork/JSONParser.swift b/OpenMarket/OpenMarket/NetWork/JSONParser.swift index 730579d..b1f0093 100644 --- a/OpenMarket/OpenMarket/NetWork/JSONParser.swift +++ b/OpenMarket/OpenMarket/NetWork/JSONParser.swift @@ -7,12 +7,12 @@ import Foundation -struct JSONParser: JSONDecodable { +class JSONParser: JSONDecodable { var product: Product? - func getProductData() throws -> Product? { + func getMockData() throws -> Product? { let fileName = "products" - guard let mock = try? mockParsingData2(name: fileName, data: product) else {throw NetWorkError.unknownError} + guard let mock = try? mockParsingData(name: fileName, data: product) else { throw NetWorkError.unknownError } return mock } } diff --git a/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift index 967660a..28fb4c9 100644 --- a/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift +++ b/OpenMarket/OpenMarket/Protocol/JSONDecodable.swift @@ -7,19 +7,17 @@ import UIKit -protocol JSONDecodable { - -} +protocol JSONDecodable { } extension JSONDecodable { - func mockParsingData2(name: String, data: T) throws -> T? { - guard let dataAsset = NSDataAsset(name: name) else { - throw JSONParsingError.jsonDecodingError - } - let jsonDecoder = JSONDecoder() - guard let data = try? jsonDecoder.decode(T.self, from: dataAsset.data) else { - throw JSONParsingError.jsonDecodingError - } - return data + func mockParsingData(name: String, data: T) throws -> T? { + guard let dataAsset = NSDataAsset(name: name) else { + throw JSONParsingError.jsonDecodingError + } + let jsonDecoder = JSONDecoder() + guard let data = try? jsonDecoder.decode(T.self, from: dataAsset.data) else { + throw JSONParsingError.jsonDecodingError } + return data + } }