Massive errors

Discussion in 'iOS Programming' started by jonanders89, Jun 14, 2011.

  1. jonanders89, Jun 14, 2011
    Last edited: Jun 14, 2011

    jonanders89 macrumors newbie

    Joined:
    Jun 14, 2011
    #1
    Hello! First post ever on this forum.

    However, i have some big problems in my app development. I'm coming from C++, but I'm now trying to get the hang of Objective-c.

    So my problem is:

    I'm trying to make kind of an statictics(i'm aware of the wrong spelling) multiview app. I've managed to get the different views working, but thats not really the problem.

    In a seperate header(and m) file, i have my class. like this;

    Code:
    @interface MyClass : NSObject
    {
    	NSString *name;
    	int someValue;
    	
    }
    I've instanced a global variabel (i know its bad) like:
    Code:
     NSMutablearray *myArray;
    So in one view, i want to take input from the user, and add an Object of MyClass to myArray. Like: (the short version)

    Code:
    -(IBAction) someMethod:(id)sender {
       MyClass *p=[[MyClass alloc]init];
       [p setName:self.'someTextField'.text];  (ofcourse without the " ' " )
    		
    		
       [myArray addObject : p];
    
      
       'someTextField'.text=nil;
    		
    [p release];
    }
    So far, so good i hope.
    Now i'm trying to print all this shizz out in another view (in a textview)

    Code:
    -(IBAction) someMethod2:	(id)sender{
    	int size=[gListe count];
    	someTextView.text=nil;
            NSMutableString *aString =[[NSMutableString alloc]init];
    	if (size>0) {
    		for (int i=0 ; i<size ; i++) {
                         [aString appendString: [myArray objectAtIndex:i].name];
                         [aString appendString: @"\n"]; 
                    }
    	 someTextView.text=aString; 
             [aString release];     	
    	}
    
    }

    This last method causes bad access about every time, sometimes it workes if it is only 1 object in myArray.
    I know its like tons of errors here. Please help!!
     
  2. chen8002004 macrumors newbie

    Joined:
    May 22, 2011
    #2
    Problems found:
    1. In MyClass, add @property (copy) NSString *name;
    2. Instead of "[aString appendString: [myArray objectAtIndex:i].name];”
    Code:
    MyClass *temp = [myArray objectAtIndex:i].name];
        [aString appendString: temp.name];
    Otherwise, a warning will be generated
     
  3. jonanders89 thread starter macrumors newbie

    Joined:
    Jun 14, 2011
    #3
    I've guess yo meant:
    But I'm still getting error when debugging where i try to appendString.
    However, some times its actually work when i've only added one object to the array, and haven't done anything else. I've tried about everything i can think of, including building the code differently. But it still comes down to approximately the same error.
     
  4. chen8002004 macrumors newbie

    Joined:
    May 22, 2011
    #4
    int size=[gListe count];
    This should be
    int size=[myArray count];
     
  5. jonanders89, Jun 15, 2011
    Last edited: Jun 15, 2011

    jonanders89 thread starter macrumors newbie

    Joined:
    Jun 14, 2011
    #5
    Thats just a typo I made when posting it here. Tried to edit my code so it would be easier to read, forgot changing the variable-names all the places.

    EDIT: forgot mentioning that the error is "EXC_BAD_ACESS"
     
  6. RonC, Jun 15, 2011
    Last edited by a moderator: Jun 15, 2011

    RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #6
    A couple of things I see, illustrated with a quick re-write:

    Your code:
    Code:
    NSMutableString *aString =[[NSMutableString alloc]init];
    if (size>0) {
        for (int i=0 ; i<size ; i++) {
            [aString appendString: [myArray objectAtIndex:i].name];
            [aString appendString: @"\n"]; 
        }
        someTextView.text=aString; 
        [aString release];     	
    }
    
    could be this code:
    Code:
    	
    NSMutableString *aString =[NSMutableString string];
    for (MyClass *myObj in myArray) {
        [aString appendFormat: @"%@\n", myObj.name];
    }
    someTextView.text=aString; 
    
    and could even be this code:
    Code:
    someTextView.text = [myArray componentsJoinedByString:@"\n"];
    a) One thing it took me a while to finally grok (after some strong hints by other people in this forum) is that "retain" and "release" don't mean +1 and -1 to reference counts (they do that, they don't mean that), rather they mean "HEY, I own this thing and am responsible for it" and "I no longer own it and am not responsible for it". You don't really want to own aString, you just want to use it temporarily.

    b) Since you don't want to own it, you shouldn't have to - that's where the string method of NSMutableString comes into play. Behind the scenes, it might very well be
    Code:
    [[[NSMutableString alloc] init] autorelease]
    but that's just how it works, not what it means. This meaning thing is what will keep you sane when you work with memory management.

    c) Since we're on the topic of meaning, understand the meaning of key words "alloc", "copy", and "init." There's a good description in the Memory Management Guide.

    d) You should really get to be friends with the APIs of foundation classes you use alot - classes like NSArray, NSString, and NSNumber.

    Here's some more thoughts:

    - Use the debugger; it is your friend. Set a breakpoint about where you're going to start to create your composite string. In the "output" window of the debugger, type this: po myArray. That calls the "description" method of the myArray object and will tell you exactly what is in your array.

    - For now, add some logging output to your program if you don't want to step through to debugger or aren't really that comfortable in the debugger. The objective C equivalent of cout is NSLog(). The equivalent of what I said above is to add the line
    Code:
    NSLog(@"myArray = %@",myArray);
    (the %@ format specification means "print this as an object", which is probably implemented as invoking description on the object).

    You didn't post your real code, so it's hard to see exactly where the problem is occuring. One thing you can do is post the stack backtrace and actual error message you get when you run your program. We can help you interpret what you're seeing.

    Ron
     
  7. jonanders89 thread starter macrumors newbie

    Joined:
    Jun 14, 2011
    #7
    Wow, thanks Ron. Great reply.

    I'm comfortable with using the debugger, I use it all the time. Great feature.
    I'll begin reading/study Objective-C for a while, it's guite different from C++.

    Anyhow, thanks again, I really appreciate it. Hope i finally can make this shiz work!
     
  8. jonanders89 thread starter macrumors newbie

    Joined:
    Jun 14, 2011
    #8
    Btw, solved this with a Singleton class instead. Works perfectly.
     

Share This Page