2011-07-07 54 views
-1

當我嘗試從diffirent類中調用NSMutableArray時,爲什麼?當我打電話時,NSMutableArray爲空

CLASS A

.h 
@interface Dashboard : UIViewController { 
    NSMutableArray *tablearrayCREDITS; 

} 
@property(nonatomic,retain)NSMutableArray *tablearrayCREDITS; 

.m 
@synthesize tablearrayCREDITS; 

和IM與來自JSON array.This所有作品因爲它應該是數據填充它..

CLASS B

.h 
    #import "Dashboard.h" 

    @interface CreditsAndDebits : UIViewController { 


    //nothing needed here.. 

} 

.m 

@implementation CreditsAndDebits 
@synthesize selectedIndexPath; 
    NSMutableArray *thearray; 

- (void)viewDidLoad { 
    Dashboard *db = [[Dashboard alloc]init]; 
    thearray = db.tablearrayCREDITS; 
} 

//當我調試它,並設置一個斷點在我分配儀表板的位置.. tablearrayCREDITS說對象0 ??,我沒有得到它?即時通話時它爲什麼會變空?

+0

你還沒有顯示任何代碼,你將一個可變數組分配給tablearrayCREDITS,也沒有任何代碼將項添加到該數組。 – Caleb

+0

在儀表板視圖中添加對象正確..就在我打開類B的視圖之前:Nslog結果:Count of ThatArray:278 – Oblieapps

+0

您在B的-viewDidLoad中使用的數據庫是一個新對象,不相同您之前添加了278個對象的儀表板實例。 – Caleb

回答

0
- (void)viewDidLoad { 

    Dashboard *db = [[Dashboard alloc]initWithNibName:@"Dashboard" bundle:nil...........]; 


    db.tablearrayCREDITS=[[NSMutableArray alloc]init]; 

    [db.tablearrayCREDITS addObject:@"madhu"]; 

    thearray = db.tablearrayCREDITS; 


//here nslog them 
} 

//you have to create Dashboard object not mutable array 
+0

我的壞我輸入它錯了我的問題在這裏.. – Oblieapps

+0

沒問題現在檢查這個出 –

+0

我怎麼可以初始化一個NSMutablearray的筆尖..?智能不會顯示initwithnibname。 – Oblieapps

0

你調用數組是空的,因爲它可能是未初始化呢。在您調用之前,您需要初始化該視圖。您可以使用該視圖的init方法執行此操作。

或者您可以在應用程序啓動時初始化該數組。您可以在AppDelegate中的application didFinishLaunchingWithOptions中執行此操作。

相關問題