标签: nsbundle

OCUnit和NSBundle

我根据"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 …
Run Code Online (Sandbox Code Playgroud)

resources unit-testing ocunit nsbundle

38
推荐指数
1
解决办法
9821
查看次数

如何获取捆绑ID?

我正在努力让facebook API适用于iOS,FB注册页面需要项目的Bundle ID.我怎样才能获得这些信息?我读了一些关于在运行时获取它的内容,但有没有办法让Xcode告诉我这些信息,或者它是否存在于某个地方.我没有使用默认的包ID,我记得在创建项目时设置它.

nsbundle ios

36
推荐指数
5
解决办法
6万
查看次数

NSBundle.mainBundle().pathForResource返回nil

我正在尝试为Swift编写一个简单的IO包装器.

为了测试这个,我在项目根目录中有一个名为"Test.txt"的文件.

我已将此文件添加到Build Bundle Resources中的Build Phases,正如遇到此问题的其他人所建议的那样.

构建捆绑资源

我已经实现了一个非常简单的File类,其中包含一个读取函数,目的是输出文件的内容.

class File2{
    let resourceName: String
    let type: String
    let bundle = NSBundle.mainBundle()


    init(resourceName: String, type: String = "txt"){
        self.resourceName = resourceName
        self.type = type
        println(self.bundle)
    }

    func read(){
        let path = self.bundle.pathForResource("Test.txt", ofType: "txt") //Hard coded these in just to make sure Strings contained no whitespace
        println(path) //This returns nil...why?
        var error:NSError?
        //print(String(contentsOfFile:path!, encoding:NSUTF8StringEncoding, error: &error)!)
        //return String(contentsOfFile:path!, encoding:NSUTF8StringEncoding, error: &error)!
    }
}
Run Code Online (Sandbox Code Playgroud)

当我打印捆绑包的内容时,我得到一个URI到我的文件系统上的特定位置,我假设它是模拟器中应用程序的虚拟位置.导航到它显示它确实包含我的"Test.txt"文件.

现在,我想要做的就是获取该文件的路径.

我这样做是通过调用: self.bundle.pathForResource("Test.txt", ofType: "txt")

这返回"nil"

为什么?:)

objective-c nsbundle ios swift

32
推荐指数
4
解决办法
4万
查看次数

在应用启动时获取捆绑文件引用/路径

假设主应用程序包中包含任意一组文件.我想获取启动时的文件URL并将它们存储在某个地方.这可能用NSFileManager吗?在这方面,文件不清楚.

注意:我只需要文件URL,我不需要访问实际文件.

nsfilemanager nsbundle ios ios5

29
推荐指数
2
解决办法
4万
查看次数

什么是目标C中的NSBundle和mainBundle?

我想问一个关于iPhone应用程序上的目标C的问题.我在apple开发者网站上阅读了一些示例程序,我发现几乎所有的应用程序都包含一个名为"NSBundle"和"mainBundle"的单词,我真的不明白这个单词的含义.谁能告诉我这件事?非常感谢你.

cocoa objective-c nsbundle

27
推荐指数
2
解决办法
2万
查看次数

如何在框架内使用Core Data的ManagedObjectModel?

我正在尝试将我的某个应用程序的特定部分迁移到框架中,以便我可以在我的应用程序本身和其中一个新的iOS 8小部件中使用它.这部分是处理Core Data中所有数据的部分.将所有内容移到并访问它是非常简单的.我只是momd在那里访问我的文件时遇到问题.

在创建时NSManagedObjectModel我仍然尝试加载momdApple的代码模板中所示:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
Run Code Online (Sandbox Code Playgroud)

不幸的是,在使用此错误访问Core Data堆栈时modelURL停留nil并因此MyApp崩溃:

2014-08-01 22:39:56.885 MyApp[81375:7417914] Cannot create an NSPersistentStoreCoordinator with a nil model
2014-08-01 22:39:56.903 MyApp[81375:7417914] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
Run Code Online (Sandbox Code Playgroud)

那么,在使用Core Data在框架内部工作时,采用这种方法的正确方法是什么?

frameworks core-data nsbundle ios nsmanagedobjectmodel

27
推荐指数
4
解决办法
1万
查看次数

在iOS上加载NSBundle文件

我想创建一个具有非常灵活的图形用户界面(skinnable)的项目.为了实现这一点,我想从外部资源(例如网站)加载NSBundle.捆绑包应包含与主项目中的某些属性和方法相对应的nib(IBOutlets和IBActions)

Apple似乎限制了以这种方式使用NSBundle的可能性.有什么方法可以让我的工作吗?如果以传统方式不可能,那么什么是可推荐的替代方法?

loading nsbundle ios

26
推荐指数
2
解决办法
2万
查看次数

如何获取结构的包?

在Swift中,你可以打电话

let bundle = NSBundle(forClass: self.dynamicType)
Run Code Online (Sandbox Code Playgroud)

在任何类中获取当前的包.如果您NSBundle.mainBundle()在例如运行单元测试时无法获得正确的捆绑.

那么如何获得Swift的当前捆绑struct

struct nsbundle swift

25
推荐指数
3
解决办法
2844
查看次数

从扩展程序中获取主应用程序包

是否可以NSBundle从应用扩展程序中获取包含应用程序?我想获取主应用程序的显示名称,而不是扩展程序的显示名称.

nsbundle ios swift ios-app-extension

23
推荐指数
2
解决办法
6462
查看次数

Swift - 获取NSBundle用于测试目标?

在Objective C中,每当我必须访问测试包时,我都会调用它

[NSBundle bundleForClass:[ClassInTestTarget class]];
Run Code Online (Sandbox Code Playgroud)

bundleForClass 在Swift中不可用,那么如何访问测试包呢?

我需要这个的原因是,为了编写我正在研究的框架的测试,我必须使用内存核心数据堆栈.最初所有资源都包含在主目标和测试目标中,但之后我将所有这些测试帮助程序仅移动到测试目标,现在下面的代码返回nil,这导致我的所有测试失败

let modelURL = NSBundle.mainBundle().URLForResource("Model", withExtension: "momd")
Run Code Online (Sandbox Code Playgroud)

nsbundle swift

21
推荐指数
1
解决办法
1万
查看次数