开发者

Pointer confusion

开发者_Python百科I am confused about one pointer situation explained below;

In class A

@property(nonatomic,retain)NSMutableArray *words;

In class B

 @property(nonatomic,retain)NSMutableArray *words;

 //creating a pointer to words from classB
 self.words = [classB words];

Now if I add new word to words array in Class A, why don't I see that word in words array in class B? I thought my words array in Class B is a pointer to Class A words?


Basically the changes should reflect in both arrays, until you change the reference of any of the two objects some where in your code.

//creating a pointer to words from classB
self.words = [classB words];

And some where in your Class A or Class B,

self.words = someOtherArray;

This will make the words array of both the classes to point different objects.


is it the code you try to run ? If yes, it seems there is a mystake in your class B code since words in class B is the same as [classB words]. (in class B : self.words = [classB words]). Maybe an instruction like : self.words = [classA words].... should resolve your problem (assuming classA is an object of class A).


As some of you guys here said it should work and it is. I had some errors in my code. But wanted to give a little example code to prove that this is how it should be:

#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

//Class 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

The result is:

2011-07-04 12:38:33.759 Pointers[20059:707] one
2011-07-04 12:38:33.767 Pointers[20059:707] two
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜