2012-04-29 47 views
0

我想要一個可在整個Objective-C項目中訪問的數組,其內容可以在必要時進行更改。我的問題是,當我從另一個類調用數組時,我總是得到null,這不是我想要的。從不同的類調用時無法訪問NSMutableArray的內容

在我.h文件中我有

@interface MainScreen2 : UIViewController 
@property (nonatomic, strong) NSMutableArray *Judith; 

,並在.m文件的viewDidLoad中功能我:

@interface MainScreen2() 

@end 

@implementation MainScreen2 
@synthesize Judith; 


- (void)viewDidLoad 
{ 
    self.Judith = [[NSMutableArray alloc]  initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];              
    [super viewDidLoad]; 
} 

,這是好的。

在一個單獨的I類有:

#import "MainScreen2.h" 

@interface NewGame() 
@end 

- (void)viewDidLoad 
{ 
    MainScreen2 *testJudith; 

    NSMutableArray *testJudithArray = [[NSMutableArray alloc]init]; 
    testJudithArray = [testJudith.Judith mutableCopy]; 
    NSLog(@"Jud test: %@", [testJudith.Judith objectAtIndex:1]); 
} 

NSLog返回空值對於這一點。這是因爲當我從MainScreen.h文件調用Judith數組時,它在這一點上是空的,因爲它尚未加載?

如果是這樣,任何人都可以幫助我在哪裏我應該把數組,所以當我打電話它,我保留它的原始內容?


編輯:4月30日

使用的好心把這個網頁現在我已經整理出了問題,現在可在建議組合。

I changed the code to the following: 

- (void)viewDidLoad 
{ 
MainScreen2 *testJudith = [[MainScreen2 alloc]init]; 
[testJudith viewDidLoad]; 
NSString *test = [testJudith.Judith objectAtIndex:1]; 
NSLog(@"Jud test: %@", test); 
} 

感謝所有對論壇帖子的貢獻!

+0

您不應該調用'-viewDidLoad' direc真的,你應該讓它被'-loadView'調用。直接調用可能會導致問題。 – 2012-05-01 19:37:51

回答

0

viewDidLoad是不一樣的init .. 。您要麼移動分配的NSArray在init或您撥打[testJudith viewDidLoad];

+0

你不應該直接調用'-viewDidLoad',你應該讓它從'loadView'被調用。 – 2012-05-01 19:38:19

0

你說過:MainScreen2 *testJudith;哪些設置testJudith爲零。

然後,您可以創建一個數組:

NSMutableArray *testJudithArray = [[NSMutableArray alloc]init]; 

然後你RESET testJudithArray到testJudith的朱迪思陣列的一個可變副本:

testJudithArray = [testJudith.Judith mutableCopy]; 

但testJudith爲零。你沒有把它設置爲任何東西。這意味着零的屬性將永遠是/返回零。然後,您嘗試製作零的可變拷貝。因此,testJudithArray變爲零。

你還沒有創建testJudith開始,所以你永遠不會創建你要求做一個可變副本的數組。 mutableCopy方法返回nil(因爲發送給nil的任何消息都返回nil)。

這是否足以說明您的錯誤在哪裏?

+0

從什麼時候'MainScreen2 * testJudith;'在函數範圍內初始化它到'nil'?這是新的嗎? – 2012-04-29 12:30:20

+0

聲明一個Objective-C值而不設置它會自動將其設置爲零。所以我可以把隨機*隨機;並且可以保證隨機實例變量在這個階段是零。在ARC中,它被定義爲明確的行爲。 – thebarcodeproject 2012-04-29 12:34:18

+1

是否有這方面的文檔? (它絕對*在ARC之前沒有這樣做)。 – 2012-04-29 12:36:44

1

讓我們看看這個代碼:

MainScreen2 *testJudith; 
NSMutableArray *testJudithArray = [[NSMutableArray alloc]init]; 
testJudithArray = [testJudith.Judith mutableCopy]; 

有兩種嚴重問題與此代碼。

  1. testJudithArray被初始化,然後再次被初始化。第一個值被丟棄。除非你使用ARC,否則就是內存泄漏。無論哪種方式,都沒有必要對它進行兩次初始化。

  2. testJudith不是初始化。如果你很幸運,程序會崩潰。你不走運,所以程序給你的結果不正確。

您將不得不初始化testJudith爲您的代碼工作。

+2

很好的答案,但如果你發送一條消息給nil(因爲所有ARC對象都被設置爲自動),那麼#2就會默默地返回nil。所以如果你把它分解爲'[[testJudith Judith] mutableCopy]'它評估爲'[nil mutableCopy]'評估爲'nil',這是一個有效的值分配給一個可變數組。 – 2012-04-29 19:05:45

+0

會給你一個去,並報告給你 - 謝謝! – user1309044 2012-04-30 08:13:13

0

嘗試使用單例。你可以谷歌單身,以獲得更多的信息。使用單例將允許您從項目的任何位置訪問相同的字符串,數組等。

通過添加一個新的Objective-C類並使其成爲NSObject的子類來創建一個單例。例如:

#import <Foundation/Foundation.h> 
@interface MyClass : NSObject 
+(MyClass *)sharedInstance; 
@end 

#import "MyClass.h" 
@implementation MyClass 
+ (MyClass *) sharedInstance 
{ 
if (!_sharedInstance) 
{ 
    _sharedInstance = [[MyClass alloc] init]; 
} 
return _sharedInstance; 
} 
@end 

如果MyClass中創建您的NSMutableArray,這將是自其他類程序的訪問,只要你在類的.m文件#IMPORT「MyClass的」你想訪問單身。

爲了從另一個類訪問MyClass的數組,這樣做:

MyClass *myClass = [MyClass sharedInstance]; 
NSString *myString = [[myClass someArray] objectAtIndex:1]; 

的東西添加到您的MyClass的數組,這樣做:

[[myClass someArray] addObject:@"something"]; 
相關問題