tags:

views:

180

answers:

3

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

+1  A: 

The correct way is:

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

You can use NSNumber instead of NSString, which might be preferable depending on your context.

You do need to create a new object everytime, because addItemWithObjectValue: is expecting an object rather than a primitive.

You can create a new object (e.g. `NSString), via two methods:

  1. Using alloc/init, like how you did it initially. Such initializations require the release of the object once it isn't required anymore in the allocation scope, using release rather than dealloc.

  2. Using stringWithFormat: factory methods that use auto release pool to release themselves "automatically". The code would look like:

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

    However, it is recommended not to use such construction within loops.

For memory issues, check out the Memory Management Programming Guide for Cocoa

notnoop
+3  A: 

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

Darren
A: 

Based on the code you posted and your stated experience level, I recommend going through Apple's Currency Converter tutorial if you haven't already. It's the standard Cocoa tutorial every beginner should read. Fundamentals like interacting with IBOutlets are covered.

Preston