Objective-C 中的保留-释放

发布于 2024-11-17 02:10:33 字数 1407 浏览 4 评论 0原文

我是 Objective-C 的新手,我对保留释放的事情感到困惑。参数会自动保留吗?我需要释放它们吗?

这是我的代码。我是否正确地完成了保留-释放的操作(以及其他所有操作)?

#import "ACStringTokenizer.h"


@implementation ACStringTokenizer

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (id)initWithStr:(NSString *)theString
{
    self = [super init];
    if (self) {
        string = [theString retain];
        delimiters = @" ";
        doesReturnDelims = NO;
    }

    return self;
}

- (id)initWithStr:(NSString *)theString andDelims:(NSString *)theDelimiters
{
    self = [super init];
    if (self) {
        string = [theString retain];
        delimiters = [theDelimiters retain];
        doesReturnDelims = NO;
    }

    return self;
}

- (id)initWithStr:(NSString *)theString andDelims:(NSString *)theDelimiters andDoesReturnDelims:(BOOL)returnDelims
{
    self = [super init];
    if (self) {
        string = [theString retain];
        delimiters = [theDelimiters retain];
        doesReturnDelims = returnDelims;
    }

    return self;
}

- (int)countTokens
{
    return numberOfTokens;
}

- (BOOL)hasMoreTokens
{
    return ![queue isEmpty];
}

- (NSString *)nextToken
{
    return [queue remove];
}

- (void)dealloc
{
    [string release];
    [delimiters release];
    [queue release];

    [super dealloc];
}

@end

提前致谢。

PS 如何让不带参数的init无效?

I am new to Objective-C and I am confused about this retain-release thing. Are parameters retained automatically? Do I need to release them?

Here is my code. Did I do the retain-release thing (and everything else) correctly?

#import "ACStringTokenizer.h"


@implementation ACStringTokenizer

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (id)initWithStr:(NSString *)theString
{
    self = [super init];
    if (self) {
        string = [theString retain];
        delimiters = @" ";
        doesReturnDelims = NO;
    }

    return self;
}

- (id)initWithStr:(NSString *)theString andDelims:(NSString *)theDelimiters
{
    self = [super init];
    if (self) {
        string = [theString retain];
        delimiters = [theDelimiters retain];
        doesReturnDelims = NO;
    }

    return self;
}

- (id)initWithStr:(NSString *)theString andDelims:(NSString *)theDelimiters andDoesReturnDelims:(BOOL)returnDelims
{
    self = [super init];
    if (self) {
        string = [theString retain];
        delimiters = [theDelimiters retain];
        doesReturnDelims = returnDelims;
    }

    return self;
}

- (int)countTokens
{
    return numberOfTokens;
}

- (BOOL)hasMoreTokens
{
    return ![queue isEmpty];
}

- (NSString *)nextToken
{
    return [queue remove];
}

- (void)dealloc
{
    [string release];
    [delimiters release];
    [queue release];

    [super dealloc];
}

@end

Thanks in advance.

P.S. How do I make init with no parameters invalid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不打扰别人 2024-11-24 02:10:33

这可能更适合http://codereview.stackexchange.com

无论如何,有几点:

  • 您应该阅读 指定初始化器。在您的情况下,您可能会将 initWithStr:andDelims:andDoesReturnDelims: 设为指定的初始值设定项。只有此初始化程序可以调用[super init]。所有其他初始化程序都调用 [self initWithStr:andDelims:andDoesReturnDelims:] 而不是 [super init]

  • 有一些更复杂的方法可以使 init 无效,但如果您想禁用它,我只需让它返回 nil 即可。然而,我真的不明白为什么你想在这种特殊情况下这样做。

  • init 方法中的保留和 dealloc 方法似乎没问题。方法参数在方法结束之前一直有效,如果您想保留它们,例如在实例变量中,您需要保留它们(您似乎已经正确完成了)。

但是,您的示例中省略了很多代码,因此显然我只是对您发布的内容进行评论。

This might be better suited to http://codereview.stackexchange.com?

Anyway, a few points:

  • You should read up on the concept of Designated Initializer. In your case you'd probably make initWithStr:andDelims:andDoesReturnDelims: the designated initializer. Only this initializer may call [super init]. All other initializers call [self initWithStr:andDelims:andDoesReturnDelims:] instead of [super init].

  • There are some more elaborate ways to make init invalid, but if you want to disable it I'd simply make it return nil. However, I don't really see a reason why you'd want to do this in this particular case.

  • The retains in your init methods and the dealloc method seem to be alright. Method parameters are valid until the end of the method, if you want to keep them beyond that, e.g. in instance variables, you need to retain them (which you seem to have done correctly).

However, there's a whole lot of code omitted in your example so obviously I'm only commenting on what you've posted.

-残月青衣踏尘吟 2024-11-24 02:10:33

参数变量不会自动保留,您只能通过引用获取对象。为了留住它们,你必须留住它们,就像你所做的那样。对我来说,你在那里所做的事情看起来非常好。

A parameter variable is not retained automatically, you only get the object by reference. To keep them you have to retain them, as you did. For me it looks perfectly fine what you did there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文