iPhone SDK:viewDidLoad()看到一个空的NSArray,但它填充在委托方法(MGTwitterEngine)中

发布于 2024-08-13 13:58:11 字数 2154 浏览 14 评论 0原文

这可能是显而易见的,但我是个新手,花了几个小时试图弄清楚这一点。

在 viewDidLoad() 中,我使用输入调用了 MGTwitterEngine 对象(自定义对象)上的函数。如果请求(在本例中为 Twitter)成功,它将调用适当的委托方法。

在 viewDidLoad() 中,它是这样的:

[myTwitterEngine getSearchResultsForQuery:@"#quote" sinceID:0 startingAtPage:0 count:10];

在成功搜索请求的情况下,它调用的委托方法如下所示:

- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
{
    NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];    

    if ([searchResults count] > 0)
    {
        for (int n=0; n < 15; n++) {

        NSDictionary *singleResult = [searchResults objectAtIndex:n];

        NSString *fromUser = [singleResult valueForKey:@"from_user"];
        NSString *text = [singleResult valueForKey:@"text"]; 

        Message *newMessage = [[Message alloc] initWithUsername:fromUser message:text];

        [tempArray addObject:newMessage];
        }

        self.messages = tempArray;

        // Okay, works here.
        NSLog(@"From delegate method: \n %@", [[self.messages objectAtIndex:13] theMessage]);
    }
}

我无法弄清楚的是: 最后,我说“好吧,工作就在这里” ,很明显 self.messages (一个 NSArray)填充了我想要的信息。

但是,如果我在第一个方法之后在 viewDidLoad() 中再次调用 self.messages ,则 self.messages 为空。这就是我在 viewDidLoad() 中所做的:

[myTwitterEngine getSearchResultsForQuery:@"#quote"];
NSLog(@"\n Before Or After? \n");
NSLog(@"From viewDidLoad: %@", [[self.messages objectAtIndex:13] theMessage]);

结果很奇怪。第一个 NSLog 的结果出现在委托方法的结果之前。这是控制台输出:

2009-12-05 23:15:20.758 Entendu[54463:207] 
 Before Or After? 
2009-12-05 23:15:20.761 Entendu[54463:207] From viewDidLoad: (null)
2009-12-05 23:15:21.005 Entendu[54463:207] Request succeeded for connectionIdentifier = 92B2E7EC-AA2F-4301-812E-E8E5AEAF7035
2009-12-05 23:15:21.007 Entendu[54463:207] From delegate method: 
 RT @thankfulnotes: RT @felicelam It usually takes me more than three weeks to prepare a good impromptu speech. Mark Twain #quote

最终我想使用消息数组作为表的数据源。我如何从实现中其他地方的委托方法中访问此变量?

任何人都知道为什么委托方法的输出会出现在最后,即使我在 NSLog 之前或之后调用了实际函数?

任何帮助表示赞赏!

This is probably obvious, but I'm a bit of a newbie and have spent hours trying to figure this out.

In viewDidLoad() I have a call to a function on an MGTwitterEngine object (custom object), with an input. If the request (to Twitter, in this case) is successful, it calls an appropriate delegate method.

In viewDidLoad(), it's like this:

[myTwitterEngine getSearchResultsForQuery:@"#quote" sinceID:0 startingAtPage:0 count:10];

The delegate method it calls in the event of a successful search request looks like this:

- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
{
    NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];    

    if ([searchResults count] > 0)
    {
        for (int n=0; n < 15; n++) {

        NSDictionary *singleResult = [searchResults objectAtIndex:n];

        NSString *fromUser = [singleResult valueForKey:@"from_user"];
        NSString *text = [singleResult valueForKey:@"text"]; 

        Message *newMessage = [[Message alloc] initWithUsername:fromUser message:text];

        [tempArray addObject:newMessage];
        }

        self.messages = tempArray;

        // Okay, works here.
        NSLog(@"From delegate method: \n %@", [[self.messages objectAtIndex:13] theMessage]);
    }
}

The thing I can't figure out is this: At the end, where I say "Okay, work's here", it's clear that self.messages (an NSArray) is populated with information I want.

However, self.messages is empty if I call it again in viewDidLoad() after the first method. This is what I've done in viewDidLoad():

[myTwitterEngine getSearchResultsForQuery:@"#quote"];
NSLog(@"\n Before Or After? \n");
NSLog(@"From viewDidLoad: %@", [[self.messages objectAtIndex:13] theMessage]);

The results are strange. The result of the first NSLog comes before the results of the delegate method. Here's the console output:

2009-12-05 23:15:20.758 Entendu[54463:207] 
 Before Or After? 
2009-12-05 23:15:20.761 Entendu[54463:207] From viewDidLoad: (null)
2009-12-05 23:15:21.005 Entendu[54463:207] Request succeeded for connectionIdentifier = 92B2E7EC-AA2F-4301-812E-E8E5AEAF7035
2009-12-05 23:15:21.007 Entendu[54463:207] From delegate method: 
 RT @thankfulnotes: RT @felicelam It usually takes me more than three weeks to prepare a good impromptu speech. Mark Twain #quote

Eventually I want to use the messages Array as a datasource for a table. How might I access this variable from within the delegate method elsewhere in the implementation?

Anyone know why delegate the output from the delegate method comes at the end even though I've called the actual function before the before-or-after NSLog?

Any help appreciated!

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

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

发布评论

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

评论(1

爱殇璃 2024-08-20 13:58:11

我对 MGTwitterEngine 一无所知,但显然 -getSearchResultsForQuery: 是一个异步方法;它会立即返回,并在一段时间后当结果可用时调用您的委托方法。

-viewDidLoad 中的数组为空,因为尚未收到结果。此外,您的数组在 -viewDidLoad 中将始终为空,因为在您将控制权返回到运行循环之前无法调用委托方法。

I know nothing of MGTwitterEngine, but clearly -getSearchResultsForQuery: is an asynchronous method; it will return immediately and call your delegate method some time later when results are available.

Your array is empty in -viewDidLoad because the results haven't been received yet. Furthermore, your array will always be empty in -viewDidLoad because your delegate method can't be called until you return control to your run loop.

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