gpt4 book ai didi

ios - NSMutableArray 在 for 循环后不保存添加的项目

转载 作者:行者123 更新时间:2023-11-29 11:05:00 25 4
gpt4 key购买 nike

我尝试调用 Facebook Graph 并将一些项目添加到数组中。这些项目被添加到一个 for 循环中,数组看起来很好。但是在 for 循环之后,项目消失了。

我将 xcode 4.5 与 Facebook SDK 3.1 结合使用。

我错过了什么?

TableViewController.m

#import "TableViewController.h"

@interface TableViewController ()
@end

@implementation TableViewController {
NSMutableArray *recipes;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
recipes = [NSMutableArray arrayWithObjects:@"Egg Benedict", nil];
[recipes addObject:@"foobar"];
NSLog(@"recipes before FBRquestConnection: %@", recipes);

[FBRequestConnection
startWithGraphPath:@"search?type=place&center=37.785834,-122.406417"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
for (id item in [result objectForKey:@"data"]) {
[recipes addObject:[item objectForKey:@"name"]];
}
NSLog(@"recipes after for-loop: %@", recipes);
NSLog(@"amount of recipes after for-loop: %u", [recipes count]);
}
}];
NSLog(@"amount of recipes after FBRequestConnection: %u", [recipes count]);
}

调试控制台输出

2012-12-27 23:09:44.056 barbar[3481:19a03] recipes before FBRquestConnection: (
"Egg Benedict",
foobar
)
2012-12-27 23:09:44.056 barbar[3481:19a03] amount of recipes after FBRequestConnection: 2
2012-12-27 23:09:44.516 barbar[3481:19a03] recipes after for-loop: (
"Egg Benedict",
foobar,
"Union Square, San Francisco",
"Benefit Cosmetics",
"San Francisco | The Official Guide",
"BareMinerals by Bare Escentuals",
"Viator.com",
"Bleacher Report",
"Downtown San Francisco",
"AMC Metreon 16",
"Cheesecake Factory",
SquareTrade,
"Westfield San Francisco Centre",
"Bloomingdale's San Francisco",
"Macy's San Francisco Union Square",
Xoom,
"Parc 55 Hotel",
"Pottery Barn",
"AT&T Park",
Bebo,
Snapfish,
"Hilton San Francisco Union Square",
uTorrent,
"The Westin St. Francis",
TRUSTe,
"Apple Retail Store - San Francisco"
)
2012-12-27 23:09:44.516 barbar[3481:19a03] amount of recipes after for-loop: 26

干杯--jerik

最佳答案

您的项目已添加到图形调用的完成 block 中。

当您执行 Api 调用时,它会转到服务器并且不会触发完成 block ,直到 Web 请求返回您的结果。

因此,当您的最后一行命中时,api 调用仍在请求中并且尚未返回。

关于ios - NSMutableArray 在 for 循环后不保存添加的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com