这是正确的方法添加项目到NSCombobox在Cocoa? [英] Is this the right way to add items to NSCombobox in Cocoa?

查看:627
本文介绍了这是正确的方法添加项目到NSCombobox在Cocoa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Delphi程序员,非常新的Cocoa。
起初我尝试过:

   - (void)awakeFromNib 
{
int i ;
NSString * mystr;
for(i = 1; i <= 24; i ++)
{
[comboHour addItemWithObjectValue:i];
}
}

但它没有工作。然后我试图在谷歌搜索,但没有运气。
经过大约30分钟的实验后,我来到这里:

   - (void)awakeFromNib 
{
int i;
NSString * mystr;
for(i = 1; i <= 24; i ++)
{
mystr = [[NSString alloc] initWithFormat:@%d,i]
[comboHour addItemWithObjectValue:mystr];
// [mystr dealloc];
}
}

我的问题是:


  1. 这是正确的方法吗?

  2. 我总是需要分配新的
    NSString从
    整数?

  3. 当我取消注释[mystr dealloc],
    为什么它不会运行




  • < >

    提前感谢

    解决方案


    需要分配新的NSString以从整数更改其值吗?


    然而,有比使用 alloc init ()更方便的创建字符串(和许多其他类型的对象)请参阅下面的自动释放池)



    您可以将任何Objective-C对象类型传递给 addItemWithObjectValue:,包括 NSString NSNumber 对象。这两个类都有一些方便的类方法可以用来创建新的实例,例如:

      for(int i = 0 ; i <24; ++ i)
    {
    [comboHour addItemWithObjectValue:[NSNumber numberWithInt:i]];
    }




    当我取消注释[mystr dealloc]它不会运行?


    不要调用 dealloc

    使用 release 引用计数,比如Delphi中的COM对象。像COM一样,当你完成一个对象时,调用 release 。当一个对象没有更多的引用时,它会自动释放。



    与COM不同,Cocoa有autorelease pools,它允许你创建一个新的NSString实例而不必担心调用 release



    例如: [NSString stringWithFormat :@%d,123] 创建一个自动释放字符串实例。完成后,您不必发布。除了 new init 方法,所有返回对象的方法都是如此。


    是否会在没有dealloc的情况下导致内存泄漏?



    我在哪里可以找到这样的基本教程?


    请参阅实用内存管理


    I'm Delphi programmer and very new to Cocoa. at first I tried this :

    -(void)awakeFromNib
    {
        int i;
        NSString *mystr;
        for (i=1;i<=24;i++)
        {
          [comboHour addItemWithObjectValue:i];
        }
    }
    

    But it didn't work. Then I tried to search on Google but no luck. After experimenting about 30 min, I come with this:

    -(void)awakeFromNib
    {
        int i;
        NSString *mystr;
        for (i=1;i<=24;i++)
        {
            mystr = [[NSString alloc]initWithFormat:@"%d",i];
            [comboHour addItemWithObjectValue:mystr];
            //[mystr dealloc];
        }
    }
    

    My questions are:

    1. Is this the right way to do that ?
    2. Do I always need to alloc new NSString to change its value from integer ?
    3. When I uncomment [mystr dealloc], why it won't run ?
    4. Does it cause memory leak to alloc without dealloc ?
    5. Where can I find basic tutorial like this on internet ?

    Thanks in advance

    解决方案

    Do I always need to alloc new NSString to change its value from integer ?

    Generally yes; however, there are more convenient ways to create strings (and many other types of objects) than using alloc and init (see autorelease pools below)

    You can pass any Objective-C object type to addItemWithObjectValue:, including NSString and NSNumber objects. Both classes have a number of convenient class methods you can use to create new instances, for example:

    for (int i = 0; i < 24; ++i)
    {
        [comboHour addItemWithObjectValue:[NSNumber numberWithInt:i]];
    }
    

    When I uncomment [mystr dealloc], why it won't run ?

    Never call dealloc. Use release instead.

    Cocoa objects are reference counted, like COM objects in Delphi. Like COM, you call release when you're finished with an object. When an object has no more references it is automatically deallocated.

    Unlike COM, Cocoa has "autorelease pools", which allows you to, for example, create a new NSString instance without having to worry about calling release on it.

    For example: [NSString stringWithFormat:@"%d", 123] creates an "autoreleased" string instance. You don't have to release it when you're done. This is true of all methods that return an object, except new and init methods.

    Does it cause memory leak to alloc without dealloc ?

    Yes, unless you're using garbage collection.

    Where can I find basic tutorial like this on internet ?

    See Practical Memory Management

    这篇关于这是正确的方法添加项目到NSCombobox在Cocoa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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