Randomizing NSArrays and autorelease

Discussion in 'iOS Development' started by Chicken, Feb 24, 2009.

  1. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Ok so I have this code
    Code:
    [[[NSArray arrayWithObjects:@"0.5", @"1.0", @"1.5", @"2.0", @"2.5", @"3.0", @"3.5", @"4.0", @"4.5", @"5.0", @"5.5", @"6.0", @"6.5", @"7.0", @"7.5", @"8.0", @"8.5", @"9.0", @"9.5", @"10.0", nil] objectAtIndex:(random() %20)]autorelease];
    I wanted to know whether I am randomizing (my new word) the array correctly and also whether I should be using autorelease?

    Thanks and I hope I am doing this right.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Unless you seed, you're going to get the same result every time. If you're trying to get a number, why use an array of strings? Also, why are you even using an array?

    Furthermore, if all you're doing is using an array to get the random result, why waste the resources using an NSArray object? Just use a standard C style array for that.

    Also, never autorelease an object that you create with a convenience constructor. Remove that and you won't get a double free error when you go to release your autorelease pool.
  3. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    ok thanks I thought you would reply first you seem to know lots of stuff about these things. I'll look into and about it showing the same result it does not always do that but it seems to show 2.5 most of the time.
    ------------------double post merged------------------
    Sorry for double posting just wondering in a few code examples people release their IB objects but in some others they do not. So my question is do I need to dealloc objects made in IB?

Share This Page