EXC_BAD_ACCESS - Trying to retrieve string from NSArray

Discussion in 'iOS Programming' started by moonman239, Sep 5, 2013.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Here's the code I'm using.

    Code:
    NSUInteger *count = 0; 
    NSArray *labelArray = [[NSArray alloc] initWithObjects:// bunch of strings, nil];
        count = count + 1;
        NSString *someString = [labelArray objectAtIndex:*count];
    
    To make sure I don't give too many details, I have modified the code, but this should be enough. Note: count is located in a .m file, just below the #import statement. The rest is in a method.

    EDIT: I'm using Xcode 4.6.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Why are you making count a pointer to an NSUInteger?
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Because I figured it would help.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    In what way?
     
  5. Sonnestah macrumors regular

    Joined:
    Mar 2, 2013
    #5
    No idea how would it help, at all. Actually it is the cause of the problem it seems.

    Btw, is count += 1 inside a loop?

    Hiding details does not help either, I am sure iterating through an array is no secret :p
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    No, that's wrong.

    Integers should be declared without a "*", an referenced without a "*" either.

    There are cases where you might use a pointer to an integer, but unless you know exactly what that "*" does, and why you're using it, don't.

    Noobies get used to declaring objects like this:

    Code:
    NSObject *someObject;
    
    And so they want to put "*"s in front of everything.

    I'm thinking that's the problem you're having.

    That syntax should be used for objects, but not for scalar types like int, bool, char, float, etc.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Duncan already addressed the majority of this but let me add: If you don't understand how something would help, don't add it or take the time to understand it well enough to know whether to add it or not.

    Based on the code you provided, no, it's not. Loops are achieved using for or while blocks.

    It's not. You should look into fast-enumeration.
     
  8. Sonnestah macrumors regular

    Joined:
    Mar 2, 2013
    #8
    You quoted the wrong person I guess
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Oops. sorry about that. Thought I was quoting the OP. My apologies. :eek:
     
  10. Sonnestah macrumors regular

    Joined:
    Mar 2, 2013
    #10
    No problems :D

    Was trying to ignite op's imagination so he could figure out whats wrong on his own :p
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    And for that, I commend you! :cool:
     
  12. moonman239, Sep 6, 2013
    Last edited: Sep 6, 2013

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #12
    Gotcha.

    Thanks, everyone!

    UPDATE: It works!
     
  13. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #13
    *I agree *although only in front of iOSsy things.


    So it was the integer "count" that is there because you thought the integer
    would help, but the stuff up was making it a pointer,
    then the OP misunderstood because he thought it was the integer that
    people were saying isn't helpful, but it's actually the pointer that people
    thought wasn't helpful.
    Then some further misunderstandings and apologies, and finally, an "It works!" :D

    So the integer is helpful?
    I would use it to address multiple arrays,
    or make a connection between an NSArray object and C array
    by indexing both arrays with "count".
    That's why I thought the >>>integer<<< might be helpful.
     

Share This Page