Re: Allocate memory for object
Re: Allocate memory for object
- Subject: Re: Allocate memory for object
- From: Andrew Merenbach <email@hidden>
- Date: Sun, 12 Dec 2004 01:32:35 -0800
Hello, Apirak. You will need a few small changes:
1. For your - getWord method, the return value type should be typed to
(NSString *) instead of (NSString). While this likely won't be causing
any of your problems, NSString by itself is not used without a pointer
reference.
2. The interface declaration for getWord should have a -, instead of a
+, for the method type. (This also likely isn't causing any of your
problems.)
3. The real problem is caused by your use of NSString *word in your
@implementation. By doing that, you are ensuring that only one
instance of the 'word' variable is created. To solve this, move
NSString *word to the area between the brackets in your @interface
section, like this:
@interface Word : NSObject {
NSString *word;
}
Also, with that in mind, you'll need to do some retain and release
stuff on your word to make sure that your Word object keeps a copy of
its word. Someone else can probably explain much better than I can
about memory management, but here's what you'd probably want to do for
your word accessors:
// We're using the method name 'word,' instead of 'getWord,' just to go
along with the standard Cocoa syntax.
- (NSString *)word {
return word;
}
- (void)setWord:(NSString *)newWord {
if (newWord != word) {
[newWord retain];
[word release];
word = newWord;
}
}
To make sure that you always have something available if your 'word'
method is called, you should also implement init and dealloc methods
for your word class:
- (id)init {
if (self = [super init]) {
word = [[NSString alloc] init]; // this creates a new, blank string
}
return self;
}
- (void)dealloc {
[word release];
word = nil;
[super dealloc]; // make sure to pass the deallocation message 'up
the chain' to NSObject
}
If you want, you could also create an initWith....: method that takes a
word or a string as its argument, to save you the time of setting the
word after you create the object.
Hope this helps,
Andrew
On 12 Dec 2004, at 00:20, Apirak wrote:
I design Word class follow my java skill, please help me to fix this
code.
//Word.h
#import <Foundation/Foundation.h>
@interface Word:NSObject{
}
+(NSString) getWord;
- (void) setWord:(NSString *)newword;
@end
//Word.m
@implementation Word
NSString *word;
//Word *left;
//Word *right;
-(NSString) getWord{
return word;
}
-(void) setWord:(NSString*)newword{
word = newword;
}
@end
at the end my Word object will contain many thing include them self
(for create binary tree of Word Object)
Thank you.
On Sat Dec 11 10:30 , Jonathan Jackel <email@hidden> sent:
I suspect the problem is in your Word class. This should work:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
NSString *word;
word = @"test0"
[myArray addObject:word];
word = @"test1"
[myArray addObject:word];
NSLog(@"Word 0: %@\nWord 1: %@", [myArray objectAtIndex:0], [myArray
objectAtIndex:1]);
I second Jonathon Mah's comments on your memory issues. It's possible
that you are not implementing setWord: or getWord properly because you
don't understand memory management yet. We might be able to help if we
saw that code.
Jonathan
On Dec 11, 2004, at 4:53 AM, Apirak wrote:
> Hello,
>
> I try to add two object to myArray by one variable. my source code is
> look like this
>
> NSMutableArray *myArray = [[NSMutableArray alloc] init];
>
> Word *word;
>
> word = [[Word alloc] init];
> [word setWord:@"test"];
> [myArray addObject:word];
>
> word = nil;
> word = [[Word alloc] init];
> [word setWord:@"test2"];
> [myArray addObject:word];
>
> Word *wd = [[myArray objectAtIndex:0] retain];
> NSLog(@"word equal %@", [wd getWord]);
>
> wd = [[myArray objectAtIndex:1] retain];
> NSLog(@"word equal %@", [wd getWord]);
>
> the result is
>
> word equal test2
> word equal test2
>
> but It should be
>
> word equal test
> word equal test2
>
> I am java developer, it very hard to deal with vector :(
>
> Apirak Panatkool
>
> e-mail: email@hidden
> phone: 01-4033320
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
email@hidden
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden