我有2個類:classA和ClassB。我想要做的是在ClassB中設置NSMutableArray * bar並在ClassA中使用它。在調試程序時,MutableArray設置了值,但是當我嘗試在ClassA中使用此值時,數組爲0.你知道我在做什麼錯誤,或者是否有更有效的方法在類之間傳遞變量值?在其他類中使用NSMutableArray屬性objective-c
示例代碼
ClassA.h
@interface ClassA : UIViewController {
NSMutableArray *bar;
}
@property(nonatomic, retain)NSMutableArray *bar;
-(IBAction)Display:(id)sender;
ClassA.m
// bar initialization
-(id)init{
self=[super init];
if (self) {
bar=[[NSMutableArray alloc]init];
}
return self;
}
-(IBAction)Display:(id)sender {
// Trying to display the NSMutableArray set in ClassB
NSLog(@"%d",[bar count]);
}
ClassB.m
#import "ClassB.h"
#import "ClassA.h"
ClassA *classA;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
classA=[[ClassA alloc]init];
// Populating the NSMUtableArray of ClassA
for (int i=0; i<[array count]; i++) {
NSDictionary* dicObjects=[array objectAtIndex:i];
NSString* remoteUrl=[dicObjects objectForKey:@"remote_url"];
[classA.bar addObject:remoteUrl];
}
// NSLog the results (It works!)
for (NSString *foo in classA.bar) {
NSLog(@"%@",foo);
}
}
你如何檢查在calssA數組是0? – jamapag
這真的是你正在運行的確切代碼嗎?您在'ClassA.m'中缺少'@ implementation'和'@synthesize bar'。 –
從哪裏調用「顯示」功能? – Shri