gpt4 book ai didi

ios - 如何快速读取 JSON.text 文件以生成数据类型?

转载 作者:行者123 更新时间:2023-11-28 14:20:21 26 4
gpt4 key购买 nike

我想模拟一个网络结果数据并在JSON.txt中模拟它,我想进行json解析的单元测试但是我似乎总是无法读取JSON.txt文件

将要测试的方法:

class Music {


func parseJSON(data : Data) -> [[String:Any]]? {

return try? JSONSerialization.jsonObject(with: data, options: []) as! [[String:Any]]
}
}

这是测试用例

import XCTest
@testable import basicUnitTesting

class musicTests: XCTestCase {


func testParseJSON() {
let music = Music()
let bundle = Bundle(for: type(of: self))
if let path = bundle.path(forResource: "JSON", ofType: "txt") {
if let data = try? Data.init(contentsOf: URL.init(fileURLWithPath: path)) {
let result = music.parseJSON(data: data)

XCTAssertNotNil(result, "It should not be nil")
XCTAssertGreaterThan(result!.count, 0, "It should have value, not zero")
} else {
XCTFail()
}

} else {
XCTFail()
}

}



}

错误截图如下:

enter image description here

如您所见,路径为 nil,然后测试失败。

这里是 JSON.txt 中用来模拟数据的数据

[{"id":"1","music_url":"http://orangevalleycaa.org/api/media/music/ModernHardRock_45450.wav","name":"Modern Hard Rock","description":"Powerful rock track with cool guitar riffs, energetic groovy drums, tight bass and guitar solo. Perfect for use in sport (like boxing) videos, advertisements etc.","duration":"2:18","image":"http://orangevalleycaa.org/api/media/images/ArtistWorking_059730538.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/ArtistWorking_059730538.png","created_by":"ikoliks","file_name_original":"Modern hard rock_LYNDA_45450.wav"},{"id":"2","music_url":"http://orangevalleycaa.org/api/media/music/AcousticIntro_45687.wav","name":"Acoustic Intro","description":"Short acoustic guitar intro with a dreamy, relaxed quality that inspires reflection.","duration":"0:57","image":"http://orangevalleycaa.org/api/media/images/BalletInSmoke_054599628.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/BalletInSmoke_054599628.png","created_by":"Trevor Lackey","file_name_original":"Acoustic Intro_LYNDA_45687.wav"},{"id":"3","music_url":"http://orangevalleycaa.org/api/media/music/OptimisticLife_41224.wav","name":"Optimistic Life","description":"Song with optimistic vibe, that's perfect background for music videos that need an uplifting and cool atmosphere.","duration":"0:30","image":"http://orangevalleycaa.org/api/media/images/BlowingGlass_066874921.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/BlowingGlass_066874921.png","created_by":"PremiumAudio","file_name_original":"Optimistic Life_LYNDA_41224.wav"},{"id":"4","music_url":"http://orangevalleycaa.org/api/media/music/TraceyLarvenz_35879.wav","name":"Light and Oxygen","description":"A bubbly, soothing track suited for corporate, marketing, and motivational videos.","duration":"3:05","image":"http://orangevalleycaa.org/api/media/images/FireDancers_042981315.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/FireDancers_042981315.png","created_by":"Tracey Larvenz","file_name_original":"Tracey Larvenz track_LYNDA_35879.wav"},{"id":"5","music_url":"http://orangevalleycaa.org/api/media/music/ExploreCaliforniaIntro_8773.wav","name":"Exploring California","description":"A synth and percussion track as smooth as a drive along the California coast.","duration":"0:20","image":"http://orangevalleycaa.org/api/media/images/GlassBlownDragon_056473995.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/GlassBlownDragon_056473995.png","created_by":"Bryce Poole","file_name_original":"Explore California Intro_LYNDA_8773.wav"},{"id":"6","music_url":"http://orangevalleycaa.org/api/media/music/Aspire_45442.wav","name":"Aspire","description":"\t\rInspiring and beautiful track played by acoustic piano, electric piano, celesta and some light oboe and clarinet sounds in the chorus.","duration":"2:45","image":"http://orangevalleycaa.org/api/media/images/GraffitiArtist_065000861.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/GraffitiArtist_065000861.png","created_by":"AudioQuattro","file_name_original":"Aspire_LYNDA_45442.wav"},{"id":"7","music_url":"http://orangevalleycaa.org/api/media/music/BeginningInTheFarm_41245.wav","name":"Beginning In The Farm","description":"Positive and inspiring. Dreams, ideas, and a sense of something new and full of beauty.","duration":"2:06","image":"http://orangevalleycaa.org/api/media/images/GraphicArtist_052214820.png","thumb":"http://orangevalleycaa.org/api/media/images/GraphicArtist_052214820.png","created_by":"Julio Kladniew","file_name_original":"Beginning In The Farm_LYNDA_41245.mp3"},{"id":"8","music_url":"http://orangevalleycaa.org/api/media/music/BrightAcousticAmbient_35443.wav","name":"Bright Acoustic Ambient","description":"Acoustic guitar and ukulele mellow track with a 12-string acoustic guitar as the main instrument.","duration":"3:21","image":"http://orangevalleycaa.org/api/media/images/PleinAirArtist_061873487.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/PleinAirArtist_061873487.png","created_by":"Eliosmusic","file_name_original":"Bright acoustic ambient_LYNDA_35443.wav"},{"id":"9","music_url":"http://orangevalleycaa.org/api/media/music/BrothersInArms_46815.wav","name":"Brothers In Arms","description":"A themed narrative piece depicting wartime heroes, victorious and fallen. Good for poignant, emotive scenes of loss and tragedy.","duration":"1:15","image":"http://orangevalleycaa.org/api/media/images/SculptorWelding_053783433.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/SculptorWelding_053783433.png","created_by":"John Leonard French","file_name_original":"Brothers In Arms_LYNDA_46815.wav"},{"id":"10","music_url":"http://orangevalleycaa.org/api/media/music/BusinessCorporateBackgrounds_41443.wav","name":"Business Corporate Backgrounds","description":"Positive, uplifting, and inspiring soft corporate ambient music that is perfect as background music for inspirational stories, advertisements, TV commercials, brand company and news, science and invention, architecture and trade, technology and education, and much more.","duration":"2:57","image":"http://orangevalleycaa.org/api/media/images/SpinningPottery_061544433.png","thumb":"http://orangevalleycaa.org/api/media/images/thumbs/SpinningPottery_061544433.png","created_by":"AZOVMUSIC","file_name_original":"Business Corporate Backgrounds_LYNDA_41443.wav"}]

最佳答案

从错误中可以清楚地看出您的文件未包含在资源路径中错误行 > if let path = bundle.path(forResource: "JSON", ofType: "txt")

因此,问题是您的文件未包含在此处 musicTests > Build Phases > Copy Bundle Resources

enter image description here

关于ios - 如何快速读取 JSON.text 文件以生成数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51964099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com