我单元测试的一些代码需要加载资源文件.它包含以下行:
NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"txt"];
在应用程序中它运行得很好,但是当单元测试框架运行时pathForResource:返回nil,意味着它找不到foo.txt.
我已经确定它foo.txt包含在单元测试目标的Copy Bundle Resources构建阶段中,为什么它找不到该文件呢?
如何在iOS上编写文件?我正在尝试使用以下代码,但我做错了:
char *saves = "abcd";
NSData *data = [[NSData alloc] initWithBytes:saves length:4]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"];
[data writeToFile:appFile atomically:YES];
我在资源上创建了MyFile.txt.
我正在尝试使用TDD和新的XCTest框架编写iOS应用程序.我的一个方法从Internet检索文件(给定NSURL对象)并将其存储在用户的文档中.该方法的签名类似于:
- (void) fetchAndStoreImage:(NSURL *)imageUrl
我正在尝试为这种方法编写测试,如果没有连接到互联网就不会失败.我的方法(取自上一个问题)是使用NSURL将方法调用到本地文件系统中的图像.
当创建启用了单元测试的新项目时,Tests目录有一个名为"Supporting Files"的子目录.我想这是我的测试图像应该去的地方.我的问题是如何获得指向此目录中的图像的NSURL对象,因为我不希望测试图像与应用程序捆绑在一起.任何帮助表示赞赏.
看起来:
[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png"];
不为Images.xcassets资产目录中的资产返回任何内容.我也尝试过:
[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images"];
[[NSBundle mainBundle] pathForResource:@"name" ofType:@"png" inDirectory:@"Images.xcassets"];
但这些都没有.
有没有人成功检索目录中的资产路径?
我有一个Xcode 5单元测试项目和一些与之相关的测试xml文件.我尝试了一堆方法,但我似乎无法加载xml文件.
我尝试过以下哪些不起作用
NSData* nsData = [NSData dataWithContentsOfFile:@"TestResource/TestData.xml"];
NSString* fileString = [NSString stringWithContentsOfFile:@"TestData.xml" encoding:NSUTF8StringEncoding error:&error];
此外,如果我尝试使用[NSBundle allBundles]预览所有捆绑包,单元测试包不会出现在列表中?
我试图创建一个单独的资源包,我似乎无法以编程方式找到它,尽管它确实已经构建和部署.
我究竟做错了什么 ?
我尝试获取名为"temp.pdf"的文件的文件路径,该文件位于NSTemporaryDirectory文件夹中(我已检查过,文件存在).
我用这个:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf" inDirectory:NSTemporaryDirectory()];
我尝试过:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp.pdf" ofType:@"" inDirectory:NSTemporaryDirectory()];
而且:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf"];
但似乎它不起作用,返回值始终为null.
所以,我有点困惑,有人可以帮助我吗?
谢谢.
对不起,今天愚蠢的问题2.是否可以确定App Bundle中是否包含文件?我可以访问文件没问题,即
NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
但无法弄清楚如何检查文件是否存在于那里.
问候
戴夫
我paylines.txt在名为的文件夹中添加了一个文件,该文件TextFiles位于ResourcesXcode中我的iOS项目的文件夹中.
这是我用来访问文件的代码:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"paylines" ofType:@"txt" inDirectory:@"TextFiles"];
NSLog(@"\n\nthe string %@",filePath);
代码打印:
2011-06-07 14:47:16.251 slots2[708:207] 
the string  (null)
我已经构建了一个静态库,它大量使用了Core Data框架.我可以在我的外部项目中成功使用该库,但仅限于我在主项目中包含.xcdatamodel文件.这不太理想,因为库的要点是尽可能隐藏实现细节.
在一个单独的问题中,我被告知我无法将资源与库捆绑在一起(这对我来说完全合情合理).
那么有没有办法以编程方式允许模型被"发现",而不必将模型包含在主项目中?
我根据"iPhone开发指南"创建了OCUnit测试.这是我要测试的类:
// myClass.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface myClass : NSObject {
    UIImage *image;
}
@property (readonly) UIImage *image;
- (id)initWithIndex:(NSUInteger)aIndex;
@end
// myClass.m
#import "myClass.m"
@implementation myClass
@synthesize image;
- (id)init {
    return [self initWithIndex:0];
}
- (id)initWithIndex:(NSUInteger)aIndex {
    if ((self = [super init])) {
        NSString *name = [[NSString alloc] initWithFormat:@"image_%i", aIndex];
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
        image = [[UIImage alloc] initWithContentsOfFile:path];
        if (nil == image) {
            @throw [NSException exceptionWithName:@"imageNotFound"
                reason:[NSString stringWithFormat:@"Image (%@) with path \"%@\" for …