2012-06-02 144 views
0

可能重複:
Images in Uiscroll cell populated by mysql database空Nsmutable陣列iPhone

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

請在此處發帖時更加清楚您的問題。 –

+0

@MichaelDautermann,它是相同的代碼,不同的問題。我發佈了兩次,因爲我得到了部分答案..謝謝 – user1430825

+0

@ShaggyFrog ..謝謝你的快速回應..這些可變數組objec1和object2有對象(帶對象的URL字符串)對象1有照片和對象的名稱2有照片的網址。我想獲得這些細節,例如照片的名稱。但是這裏的NSMutable數組返回空。這是清楚的嗎? – user1430825

回答

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返回類型)。