正如你們中的一些人在這裏所說的那樣,它應該是有效的。我的代碼中有一些錯誤。不過想給一個小例子代碼來證明這是應該的:
#import "PointersAppDelegate.h"
#import "PointersViewController.h"
#import "ClassA.h"
#import "ClassB.h"
@implementation PointersAppDelegate
@synthesize window=_window;
@synthesize viewController=_viewController;
@synthesize classA, classB;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
classA = [[ClassA alloc] init];
classB = [[ClassB alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
//Add new word to words in classA
[[classA words] addObject:@"two"];
//Print words in classB
[classB print];
return YES;
}
- (void)dealloc
{
[_window release];
[_viewController release];
[classA release];
[classB release];
[super dealloc];
}
@end
// A類
// ClassA.h
@interface ClassA : NSObject {
NSMutableArray *words;
}
@property(nonatomic,retain)NSMutableArray *words;
@end
// ClassA.m
#import "ClassA.h"
@implementation ClassA
@synthesize words;
- (id)init{
self = [super init];
if (self) {
words = [[NSMutableArray alloc] initWithObjects:@"one", nil];
}
return self;
}
- (void)dealloc {
[words release];
[super dealloc];
}
@end
ClassB的
// ClassB.h
#import <Foundation/Foundation.h>
@interface ClassB : NSObject {
NSMutableArray *words;
}
@property(nonatomic,retain)NSMutableArray *words;
-(void)print;
@end
// ClassB.m
#import "ClassB.h"
#import "PointersAppDelegate.h"
@implementation ClassB
@synthesize words;
- (id)init{
self = [super init];
if (self) {
self.words = [[(PointersAppDelegate*)[[UIApplication sharedApplication] delegate] classA] words];
}
return self;
}
-(void)print{
for(int i=0;i<[words count];i++)
NSLog(@"%@", [words objectAtIndex:i]);
}
- (void)dealloc {
[words release];
[super dealloc];
}
@end
結果是:
2011-07-04 12:38:33.759 Pointers[20059:707] one
2011-07-04 12:38:33.767 Pointers[20059:707] two
兩者都屬於不同的類別。那麼怎麼可能呢? –
@Cyprian classB中的'''屬性是在你做任務的時候實例化的嗎? –
它應該工作。你的代碼中還有其他的東西會導致數組不同。 – 2011-07-04 10:09:02