-(void)getItems
{
NSString *root = URL
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/phpfile", root]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
[self.object removeAllObjects];
id results = [JSON valueForKeyPath:@"ids"];
[results enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
[self.object addObject:[NSString stringWithFormat:@"%@",[obj valueForKey:@"item1"]]];
[self.object2 addObject:[NSString stringWithFormat:@"%@",[obj valueForKey:@"item2"]]];
-(void)DidLoad
{
//initializing arrays to hold data
//Initiate arrays that hold data
object1 = [[NSMutableArray alloc] init];
object2 = [[NSMutableArray alloc] init];
[getItems];
//Why do I get empty arrays here e.g object 1 and 2 are returning empty ?
问问题
103 次
1 回答
1
假设这是您的问题:
//Why do I get empty arrays here e.g object 1 and 2 are returning empty ?
这段代码:
//Initiate arrays that hold data
object1 = [[NSMutableArray alloc] init];
object2 = [[NSMutableArray alloc] init];
在堆上动态创建新数组。它们自然是空的,因为它们刚刚被创造出来。
这一行:
[getItems];
我认为您正在尝试调用getItems
上面发布的方法,但您没有在方法调用中引用对象。你要:
[self getItems];
即便如此,你仍然有问题;假设get
名称的一部分应该得到一些东西(项目),你不会从方法中返回任何东西(用void
返回类型标记)。
于 2012-06-02T05:54:47.043 回答