如何在更新後訪問@public NSArray
或NSMutableArray
?從一個班級到另一個班級?可可:訪問@public NSArray將其從一個類更新到另一個類
我可以通過調用-(id)init
來訪問它..當程序啓動時,但如果我想稍後更新它,我會得到零。有什麼問題?
簡單示例代碼:
MyFirstClass.h
#import <Foundation/Foundation.h>
@interface MyFirstClass : NSObject
{
@public
NSArray *testArray;
}
-(IBAction)Button:(id)sender;
@end
MyFirstClass.m
#import "MyFirstClass.h"
@implementation MyFirstClass
- (id)init {
if (self = [super init]) {
//I can take this Array from here
//testArray = [NSArray arrayWithObjects: @"first", @"second", @"third", nil];
}
return self;
}
-(IBAction)Button:(id)sender {
//How take this Array?
testArray = [NSArray arrayWithObjects: @"first", @"second", @"third", nil];
}
@end
SimpleClass.h
#import <Foundation/Foundation.h>
#import "MyFirstClass.h"
@interface SimpleClass : NSObject
{
MyFirstClass *other;
}
-(IBAction)ButtonGet:(id)sender;
@end
SimpleClass.m
#import "SimpleClass.h"
#import "MyFirstClass.h"
@implementation SimpleClass
-(IBAction)ButtonGet:(id)sender {
other = [[MyFirstClass alloc] init];
if(other->testArray) {
NSLog(@"Working!");
}
else { NSLog(@"Not working!"); }
}
@end
創建的示例在* .xib中有兩個按鈕。一個名爲「Button」(MyFirstClass)和其他「ButtonGet」(SimpleClass)的按鈕。所以當程序開始時需要按「Button」然後「ButtonGet」,之後NSLog應該寫「Working!」如果它得到testArray,如果沒有 - 「不工作!」。它總是顯示「不工作!」。
請編輯您的問題,併發布代碼,您聲明實例變量,爲其分配一個數組實例,並在其中嘗試使用它。另外,您是否使用垃圾回收,ARC或手動內存管理? – 2011-11-03 23:49:53
感謝您的評論,我沒有使用垃圾回收,我正在使用手動內存管理。 –
在'awakeFromNib'中放置一個斷點,然後在調試器面板中右鍵單擊'testArray'並選擇「Watch Variable」。這樣,下次你的變量被修改,調試器會暫停你的程序。 – zneak