iOS 对象数组:在函数内创建对象数组

发布于 2024-11-08 03:39:45 字数 323 浏览 0 评论 0原文

我有一个“Note”类,还有一个 Tuner 类。我的 Tuner 类正在其 init 函数中初始化一个 NSArray*

我想初始化这个数组来容纳 88 个音符,现在我正在创建像 Note* key1; 这样的东西。等等等等。

我只是确保我需要为这些注释分配东西,以便它们在初始化完成后不会超出范围,并且我的 NSArray 与其条目保持一致,或者我是能够只声明 Note key1 并插入到数组中,它就为我处理好了。 创建一个包含一些对象的 NSArray 的示例代码片段,使得即使在函数退出后该数组也能保留下来,这将非常有帮助。谢谢你!

I have a "Note" class, and I have a Tuner Class. My Tuner class is initializing an NSArray* in it's init function.

I want to initialize this array to hold 88 notes, and right now I am creating things like Note* key1; etc etc.

I am just making sure that I need to alloc things for these notes so that they do not go out of scope once init finishes, and my NSArray stays in tact with its entries, or am I able to just declare Note key1 and insert into the array and it is taken care of for me.
An example piece of code creating an NSArray containing a few objects such that the array will stay around even after the function exits would be very helpful. Thank you!

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

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

发布评论

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

评论(4

浪漫人生路 2024-11-15 03:39:45

NSArrayNSMutableArray 在添加对象时将 -retain 发送给对象,因此适当的做法是 -release > 您显式添加的对象或使用自动释放的对象。

下面是 Tuner 类的 -init 方法的一个简单示例:

- ( id )init
{
    NSInteger    i;
    Note         *note;

    self = [ super init ];
    if ( self ) {
        notes = [[ NSMutableArray alloc ] initWithCapacity: 88 ];
        for ( i = 0; i < 88; i++ ) {
            note = [[ Note alloc ] initWithKey: i ];
            [ notes addObject: note ];
            [ note release ];
        }
    }

    return( self );
}

您需要 +alloc 每个 Note 对象并 -release it,因为将其添加到数组中会保留它。要在完成后销毁所有音符对象,请确保您的 Tuner 类在其 -dealloc 方法中释放数组:

- ( void )dealloc
{
    [ notes release ]; // this sends release to all objects in the array, too

    [ super dealloc ];
}

NSArray and NSMutableArray send -retain to objects when they are added, so the appropriate thing to do is to -release the objects you add explicitly or use autoreleased objects.

Here's a quick example for the Tuner class's -init method:

- ( id )init
{
    NSInteger    i;
    Note         *note;

    self = [ super init ];
    if ( self ) {
        notes = [[ NSMutableArray alloc ] initWithCapacity: 88 ];
        for ( i = 0; i < 88; i++ ) {
            note = [[ Note alloc ] initWithKey: i ];
            [ notes addObject: note ];
            [ note release ];
        }
    }

    return( self );
}

You will need to +alloc every Note object and -release it, since adding it to the array retains it. To destroy all the note objects when you're done, make sure your Tuner class releases the array in its -dealloc method:

- ( void )dealloc
{
    [ notes release ]; // this sends release to all objects in the array, too

    [ super dealloc ];
}
看轻我的陪伴 2024-11-15 03:39:45

下面是一个简单的示例,说明如何在 Tuner 类的 init 函数中执行此操作。

for ( int i = 0; i < 88; ++i )
{
    Note *key = [[Note alloc] init];
    [myNotes addObject:key];
    [key release];
}

请注意,在我的示例中,myNotes 被假定为 类型NSMutableArray 这是通过像这样单独添加对象来构造数组的唯一方法。

Here's a simple example of how you might do that in the init function of your Tuner class

for ( int i = 0; i < 88; ++i )
{
    Note *key = [[Note alloc] init];
    [myNotes addObject:key];
    [key release];
}

Please note that in my example, myNotes is assumed to be of the type NSMutableArray It is the only way you can construct an array by adding objects individually like that.

寂寞花火° 2024-11-15 03:39:45

在保存笔记数组的类中。

  1. 使用 NSMutableArray 类型的(nonatomic,retain)创建一个名为notes的属性。
  2. 合成它。
  3. 确保它在dealloc方法中被释放。

在创建数组的方法内部:

  1. init 并分配一个新的临时 NSMutableArray。
  2. 循环并将所有 Note 对象添加到数组中。
  3. 将此数组添加到注释中。

Inside the class which will hold the notes array.

  1. Create a property using (nonatomic, retain) of type NSMutableArray called notes.
  2. Synthesize it.
  3. Make sure it is released in the dealloc method.

Inside the method for creating the array:

  1. init and alloc a new temporary NSMutableArray.
  2. Loop and add all Note objects to the array.
  3. Adding this array to notes.
若能看破又如何 2024-11-15 03:39:45

好吧,我不完全确定你想做什么,但你可以将 NSMutableArray 作为全局变量。然后,您可以向该数组添加和删除任何类型的任何对象。该数组仍将位于内存中,直到您调用它的释放方法。

Well, I'm not totally sure what you are trying to do, but you could just make an NSMutableArray as a global variable. You could then add and remove any object of any type to this array. The array would still be in memory until you call it's release method.

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