【问题标题】:Unable to load "path" with "stringWithFormat" objective-c无法使用“stringWithFormat”objective-c 加载“path”
【发布时间】:2012-03-22 15:37:52
【问题描述】:

我有这种方法可以将我的应用程序中的 html 文件加载到 UIWebView 中。 问题是这个文件名不时改变 - 我无法让它工作。 这是我的代码:

    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:[NSString stringWithFormat:@"%@_file",MyFileNameString] ofType:@"htm"];

// make a file: URL out of the path
    NSURL *instructionsURL = [NSURL fileURLWithPath:path];
    [ProfileHtml loadRequest:[NSURLRequest requestWithURL:instructionsURL]];

我遇到了这个问题:[NSURL initFileURLWithPath:]: nil string parameter.

当我以这种方式使用它时,它的工作原理:

NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:[NSString stringWithFormat:@"%@_file",@"amir"] ofType:@"htm"];

// make a file: URL out of the path
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[ProfileHtml loadRequest:[NSURLRequest requestWithURL:instructionsURL]];

我应该怎么做才能解决它? 谢谢!

//////////更新

这是整个方法: 希望你能帮助我。

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *zodiacProfile = [defaults valueForKey:@"zodiacProfile"];    

        NSLog(@"zodiacProfile: %@",zodiacProfile);
        [zodiacWallpaper setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Wallpaper_%@",zodiacProfile]]];
        zodiacTitle.text = [NSString stringWithFormat:@"About %@",zodiacProfile];

        NSString *path;
        NSBundle *thisBundle = [NSBundle mainBundle];
        path = [thisBundle pathForResource:[NSString stringWithFormat:@"%@HTM",zodiacProfile] ofType:@"htm"];

        NSURL *instructionsURL = [NSURL fileURLWithPath:path];

        NSLog(@"zodiac: %@",zodiacProfile);
        NSLog(@"path: %@",path);
        NSLog(@"url: %@",instructionsURL);

        [ProfileHtml loadRequest:[NSURLRequest requestWithURL:instructionsURL]];

【问题讨论】:

  • 那么MyFileNameString的内容是什么?如果这是工作和不工作之间的唯一区别,那就是问题所在。
  • 在您的第一个示例中,您确定 MyFileNameString 设置为您认为的值吗?你确定你的包中存在同名的文件吗?
  • 在构建应用程序之前,您确定所有 htm 文件都已添加到正确的目标(复制文件构建阶段)?
  • 我想说你应该测试pathForResourcefileURLWithPath 的结果是否为零。
  • 当我像第一个例子一样使用它时,路径变为 nil .. 这就是问题,但我应该如何解决它?

标签: iphone objective-c uiwebview


【解决方案1】:

好的,问题是我想要集中注意力而忘记将 html 文件添加到我的项目中。 因此,对于将来某个时候需要此代码的所有人来说,它的工作非常完美。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *zodiacProfile = [默认 valueForKey:@"zodiacProfile"];

    NSLog(@"zodiacProfile: %@",zodiacProfile);
    [zodiacWallpaper setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Wallpaper_%@",zodiacProfile]]];
    zodiacTitle.text = [NSString stringWithFormat:@"About %@",zodiacProfile];

    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:[NSString stringWithFormat:@"%@HTM",zodiacProfile] ofType:@"htm"];

    NSURL *instructionsURL = [NSURL fileURLWithPath:path];

    NSLog(@"zodiac: %@",zodiacProfile);
    NSLog(@"path: %@",path);
    NSLog(@"url: %@",instructionsURL);

    [ProfileHtml loadRequest:[NSURLRequest requestWithURL:instructionsURL]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多