iOS/Objective-C:将 NSString 对象添加到 NSMutableArray,NSMutableArray 为 (null)

发布于 2024-12-13 03:44:19 字数 1121 浏览 1 评论 0 原文

- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (urlIndex == maxIndex) { 
    maxIndex = maxIndex + 1;

    NSString* sURL = webpage.request.URL.absoluteString;

    [urlHistory addObject:sURL];
    NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);
    NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);


    [sURL release];

    urlIndex = urlIndex + 1;
}
else {
    [urlHistory insertObject:webView.request.URL.absoluteString atIndex:(urlIndex - 1)];
}
}

此行

    NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);

打印 (null),而此行

    NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);

打印实际的 URL。

在我的 initWithNibName 上,我有:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"Tab1ViewController" bundle:nil];

if (self) {
    urlHistory = [[NSMutableArray alloc] init];
    urlIndex = 0;
    maxIndex = 0;
}

return self;
}

但当我访问数组时,我仍然不断收到 (null) 。这是为什么?

- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (urlIndex == maxIndex) { 
    maxIndex = maxIndex + 1;

    NSString* sURL = webpage.request.URL.absoluteString;

    [urlHistory addObject:sURL];
    NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);
    NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);


    [sURL release];

    urlIndex = urlIndex + 1;
}
else {
    [urlHistory insertObject:webView.request.URL.absoluteString atIndex:(urlIndex - 1)];
}
}

This line

    NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);

prints (null), while as this line

    NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);

prints the actual URL.

On my initWithNibName I have:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"Tab1ViewController" bundle:nil];

if (self) {
    urlHistory = [[NSMutableArray alloc] init];
    urlIndex = 0;
    maxIndex = 0;
}

return self;
}

But I still keep getting (null) when I access my array. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2024-12-20 03:44:19

听起来你的 urlHistory 对象是 nil 。对 nil 对象调用 -objectAtIndex: 将返回 nil。您可能忘记在 -init 中初始化 urlHistory,或者您实际上并没有调用您认为的 -init 方法(例如,如果您的 VC 是从笔尖加载的,它将使用 -initWithCoder: 而不是 -initWithNibName:bundle:)。

根据记录,如果 NSArray 上的 -objectAtIndex: 返回 nil,则意味着数组本身为 nil 。由于 NSArray 无法存储 nil,因此具有有效索引的 -objectAtIndex: 永远不会返回 nil,并且 -objectAtIndex: 索引无效将引发异常。因此,-objectAtIndex: 返回 nil 的唯一方法是方法本身从未被实际调用。

It sounds like your urlHistory object is nil. Calling -objectAtIndex: on a nil object will return nil. You probably forgot to initialize urlHistory in your -init, or you're not actually calling the -init method you think you are (e.g. if your VC is loaded from a nib it will be using -initWithCoder: instead of -initWithNibName:bundle:).

For the record, if -objectAtIndex: on an NSArray ever returns nil, it means the array itself is nil. Since NSArray cannot store nil, -objectAtIndex: with a valid index will never return nil, and -objectAtIndex: with an invalid index will throw an exception. So the only way for -objectAtIndex: to return nil is if the method itself is never actually called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文