Created
September 18, 2018 02:36
-
-
Save ktanaka117/2a777becd14577632361eace0cb57ff1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class TestDataHelper { | |
// 使い方 | |
// let agreement: Agreement = TestDataHelper.testData(forResource: "Some", ofType: "json") | |
static func testData<T: Decodable>(forResource resource: String, ofType type: String) -> T { | |
let url = Bundle(for: TestDataHelper.self).url(forResource: resource, withExtension: type)! | |
let data = try! Data(contentsOf: url) | |
let decoder = JSONDecoder() | |
decoder.keyDecodingStrategy = .convertFromSnakeCase | |
let decodedData: T = try! decoder.decode(T.self, from: data) | |
return decodedData | |
} | |
// 使い方 | |
// let json = """ | |
// { | |
// "screen_name": "ktanaka117" | |
// } | |
// """ | |
// let billingAgreement: BillingAgreementResponseObject = TestDataHelper.testData(fromJsonString: json) | |
static func testData<T: Decodable>(fromJsonString json: String) -> T { | |
let data = json.data(using: .utf8) | |
let decoder = JSONDecoder() | |
decoder.keyDecodingStrategy = .convertFromSnakeCase | |
let decodedData: T = try! decoder.decode(T.self, from: data!) | |
return decodedData | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment