【问题标题】:Xcode Assistance with CodingXcode 协助编码
【发布时间】:2015-06-22 20:41:55
【问题描述】:

我想弄清楚如何为 html 文件做扩展。我是编码和 Xcode 的新手,并试图创建一个应用程序,但我有足够的编码经验来理解什么去哪里以及为什么去。

我面临的问题是我需要为 html 文件添加扩展名;例子

(index.html?lc=uk) 而不是 (index.html)。

我已经创建了一个界面和所需的所有其他信息,但我似乎无法让这部分工作。我在 ViewController.m 上使用的编码是;

[super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"354"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webview loadRequest:request];

我尝试添加到 (pathForResource) @"index.html?lc=uk" 但是当我测试应用程序时它不起作用并给我一个错误。

有人可以告诉我我需要做什么吗?

亲切的问候,

Seb

[日志文件]

2015-06-22 12:48:53.845 UST Demo[3999:1153643] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(
    0   CoreFoundation                      0x008d0746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x00559a97 objc_exception_throw + 44
    2   CoreFoundation                      0x008d066d +[NSException raise:format:] + 141
    3   Foundation                          0x0010edfe -[NSURL(NSURL) initFileURLWithPath:] + 139
    4   Foundation                          0x0010ed58 +[NSURL(NSURL) fileURLWithPath:] + 68
    5   UST Demo                            0x000768d5 -[ViewController viewDidLoad] + 213
    6   UIKit                               0x00df3da4 -[UIViewController loadViewIfRequired] + 771
    7   UIKit                               0x00df4095 -[UIViewController view] + 35
    8   UIKit                               0x00ce5e85 -[UIWindow addRootViewControllerViewIfPossible] + 66
    9   UIKit                               0x00ce634c -[UIWindow _setHidden:forced:] + 287
    10  UIKit                               0x00ce6648 -[UIWindow _orderFrontWithoutMakingKey] + 49
    11  UIKit                               0x0c5c9028 -[UIWindowAccessibility _orderFrontWithoutMakingKey] + 77
    12  UIKit                               0x00cf49b6 -[UIWindow makeKeyAndVisible] + 80
    13  UIKit                               0x00c89ed8 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3217
    14  UIKit                               0x00c8d422 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639
    15  UIKit                               0x00ca693e __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
    16  UIKit                               0x00c8c04a -[UIApplication workspaceDidEndTransaction:] + 155
    17  FrontBoardServices                  0x03232c9e __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
    18  FrontBoardServices                  0x0323272f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
    19  FrontBoardServices                  0x03244d7c __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 30
    20  CoreFoundation                      0x007f2050 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
    21  CoreFoundation                      0x007e7963 __CFRunLoopDoBlocks + 195
    22  CoreFoundation                      0x007e77bb __CFRunLoopRun + 2715
    23  CoreFoundation                      0x007e6a5b CFRunLoopRunSpecific + 443
    24  CoreFoundation                      0x007e688b CFRunLoopRunInMode + 123
    25  UIKit                               0x00c8ba02 -[UIApplication _run] + 571
    26  UIKit                               0x00c8f106 UIApplicationMain + 1526
    27  UST Demo                            0x00076dfa main + 138
    28  libdyld.dylib                       0x02c5cac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

[大麦]

[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"354"];
path = [NSString stringWithFormat:@"%@?lc=uk", path]; // add the '?lc=uk'
NSURL *url = [NSURL URLWithString:path]; // build URL with string
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

当我添加此代码时,我收到此错误

int main(int argc, char * argv[]) {
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}

}

【问题讨论】:

  • 使用调试器单步执行代码并检查值。或者为pathurl request等变量添加NSLog语句。如果您仍有问题,请将日志结果添加到问题中。
  • 我试过这样做,但是在测试应用程序时,html 根本没有打开。我需要向 index.html 添加扩展名,但是当我这样做时,文件不起作用。如果我删除它,文件工作但不显示任何内容,因为它需要扩展才能工作。我需要做的就是将 index.html?lc=uk 添加到其中,仅此而已。没有办法吗?
  • 重新阅读评论,特别是这部分:“添加日志结果的问题”。我们无法猜测您的代码的结果。
  • 日志文件好像太长了不能贴在这里:/有没有办法贴出来?
  • 我指出的东西属于调试。调试是所有开发人员都必须学习的技能。除其他事项外,您必须检查可能位于错误路径中的每条指令。这可能是乏味且耗时的,有时需要数天才能找到问题。减少调试时间的最好方法是彻底理解编写的每一行代码。

标签: html ios xcode


【解决方案1】:

如果我没听错,你需要像这样更改 NSString* 路径:

path = [NSString stringWithFormat:@"%@?lc=uk", path];

但是,如果从未尝试过用于本地路径 ([NSURL fileURLWithPath:path];),也许您应该使用 [NSURL URLWithString:path];?您是否将 HTML 文件作为资源,或者您正在尝试访问远程源?

对于本地源,最终甚至不考虑 GET 参数,因为没有安装 PHP 或其他任何东西作为 HTML 文件的处理器。

编辑:

您的来源:

[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"354"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

新来源:

[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"354"];
path = [NSString stringWithFormat:@"%@?lc=uk", path]; // add the '?lc=uk'
NSURL *url = [NSURL URLWithString:path]; // build URL with string
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

希望对你有所帮助。

【讨论】:

  • 嗨,Da Maex,很抱歉,但我不明白我需要在哪里添加 Path = [NSString。当我查看我拥有的内容时,我看不到它,抱歉我是新手,只需要一些帮助来学习。
  • 我在支持文件文件夹中有 html 文件,我要做的就是向 index.html 文件添加扩展名,仅此而已。但似乎比我想象的要多:(
  • 我想,我无法很好地解释我的问题,从而无法就我的问题获得支持。我想我会试着弄清楚,谢谢你的尝试。
  • NEW 嗨,Da Maex,我已经添加了你给我的代码,但我收到了这个错误 - int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
  • 这不是错误,它是错误发生的地方,它可能是一个分段错误(或类似的东西),所以它没有显示正确的行号。观看此developer.apple.com/videos/wwdc/2012/?id=412,了解如何使用 XCode 进行调试。或者这个 youtube 视频也非常适合初学者:youtube.com/watch?v=gPzSSeJc2oo 然后逐行调试您的代码并检查变量是否符合您的要求。
猜你喜欢
  • 2021-07-15
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多