如何在 Objective C 中初始化 NSMutableArray? [英] How to initialize a NSMutableArray in Objective C?

查看:81
本文介绍了如何在 Objective C 中初始化 NSMutableArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自 Java 背景,正在学习 Objective C.我正在尝试创建一个具有字符串数组和成员函数的类来修改数组.我的代码如下所示:

I come from a Java background and I am learning Objective C. I am trying to create a class that has a string array and a member function to modify the Array. My code looked like this:

@implementation TAWChapter

@synthesize mSubject;

@synthesize mItems;

- (id) init{
    self.mItems = [[NSMutableArray alloc] init];
    return self; 
}

- (void) setSubject:(NSString *)subject{
    self.mSubject = subject; 
}

- (void) addItem:(NSString *)item{
    [self.mItems addObject:@"asdf"]; 
}

@end

这没有用.我得到了一个 "[__NSArrayI addObject:]: unrecognized selector sent to instance " 和一个 "NSInvalidArgumentException".在互联网上搜索后,我将构造函数中的单行更改为:

Which didn't work. I got a "[__NSArrayI addObject:]: unrecognized selector sent to instance " and a "NSInvalidArgumentException". After searching on internet, I changed the single line in the constructor to:

self.mItems = [self.mItems init];

它奏效了,但为什么呢?从 Java 开发人员的角度来看,第一个比第二个更有意义.我还有另一条线,它与第一条线相同,但它正在工作(不在构造函数中).有人可以向我解释一下吗?

It worked, but why? From a Java developer's point of view the first one makes more sense than the second one. And I have another line it's the same as the first one but it's working(not in a constructor). Can someone explain this to me please?

推荐答案

  1. 首先,您应该遵守 Objective-C 编码约定.在 Objective-C 中,类没有构造函数,它们有初始化器.在 Objective-C 中,初始化器调用超类的初始化器,所以它应该是这样的:

  1. First of all, you should adhere to Objective-C coding conventions. In Objective-C, classes don't have constructors, they have initialisers. In Objective-C, initialisers invoke the initialiser of the superclass, so it should look like this:

- init
{
    self = [super init];
    if (!self) return nil;

    // set up other stuff here 

    return self;
}

  • 其次,除非您使用的是 ARC,否则您可能会出现内存泄漏.您的初始化程序的第一行将您拥有的对象分配给一个也可能拥有所有权的属性.您应该使用:

  • Second, unless you are using ARC, you might have a memory leak. The first line of your initialiser assigns an object that you own to a property that also likely takes ownership. You should use either:

    // property takes care of ownership
    self.mItems = [NSMutableArray array];
    

    或:

    // assign to instance variable directly with owned object
    mItems = [[NSMutableArray alloc] init];
    

    Apple 有时不鼓励在初始化程序中使用访问器方法,因为它可能会处理 KVO 之类的事情,但一致使用访问器方法可确保正确的对象所有权和内存管理.

    Apple sometimes discourage the use of accessor methods in initialisers because it can fiddle with things like KVO, but consistent use of accessor methods ensures proper object ownership and memory management.

    通过将初始化程序中的行更改为:

    By changing your line in your initialiser to:

    self.mItems = [self.mItems init];
    

    什么都不做.当您的初始化方法被调用时(通常是在它被分配之后),所有实例变量都会自动设置为 nil.所以你正在做的只是:

    does nothing. When your initialiser method is called (which is typically just after it has been allocated), all instance variables are automatically set to nil. So what you are doing is just:

    self.mItems = [nil init];
    

    就是:

    self.mItems = nil;
    

    并且,不要在没有先分配实例的情况下使用 init,也不要多次使用 init.

    and, don't use init without first allocating an instance, and never use init more than once.

    如果不让超类自行初始化,那么它可能会表现为其他方面的问题.做一个构建&分析并确保您已解决分析员指出的任何问题.

    If you do not let the superclass initialise itself, then it may manifest as problems in other areas. Do a Build & Analyze and ensure you have fixed up any issues pointed out by the analyser.

    这篇关于如何在 Objective C 中初始化 NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆