空的Nsmutablearray Iphone

时间:2012-06-02 05:46:35

标签: iphone uitableview nsmutablearray

  

可能重复:
  Images in Uiscroll cell populated by mysql database

-(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 ?

1 个答案:

答案 0 :(得分: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返回类型标记)。