gpt4 book ai didi

ios - 如何在 Xcode 中加载本地 json 文件以进行单元测试?

转载 作者:行者123 更新时间:2023-12-03 03:35:05 25 4
gpt4 key购买 nike

所以我已将文件 testJson1.json 添加到我的项目中。

在单元测试中,我尝试获取文件路径,但没有成功:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *filepath = [bundle pathForResource:@"testJson1.json" ofType:@"json"];

当我查看项目文件时,奇怪的是,我发现该文件已添加到资源中,我认为单元测试文件不会添加到资源中以缩小应用程序的大小。

8AA238811C5565E80031648E /* testJson1.json in Resources */ = {isa = PBXBuildFile; fileRef = 8AA2387F1C5565E80031648E /* testJson1.json */; };

请问我缺少什么?

enter image description here

最佳答案

假设您有上图中的一个名为...的测试类

final class TagNewsTests: XCTestCase {

在您的 func test() 方法中以这种方式加载 bundle ...

let url = Bundle(for: TagNewsTests.self).url(forResource: "testJson1", withExtension: "json")

关于ios - 如何在 Xcode 中加载本地 json 文件以进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980996/

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