Using initWithArray to fill NSArray from Method!

Discussion in 'iOS Programming' started by maymunaga, Jun 30, 2010.

  1. maymunaga macrumors newbie

    Joined:
    Jun 30, 2010
    #1
    Firtsly hi to everyone,

    I'm very new in Objective C programming,

    Let's start my question...

    I have method (function) which returns an array, and with this array i want to fill another array in where i want to use.

    here are the my codes.

    in this example i use it for a button when i press it creates number then showme.

    my function which name is myNumbers creates random 6 numbers 1 to 49, then put them in an array and send it.

    Code:
    +(NSArray*)myNumbers{
    	
    	int tamammi=0;
    	
    	
    	//static int gonder[6];
    	NSArray *gonder = [[NSArray alloc]init];
    	int sayac=0;	
    	while(1)
    	{
    		
    		if (sayac < 6){
    			int rnd=rand()%49+1;
    			
    			int z;
    			int varmi=0;
    			
    			for (z=0;z<6;z++){		
    				if ([gonder objectAtIndex:z]==rnd){
    					
    					varmi=1;
    					break;
    				}
    			}
    			
    			if (varmi==0){
    				[gonder addObject:[NSNumber numberWithInt:rnd]];
    				//gonder[sayac]=rnd;						
    				sayac += 1;
    			}
    			
    			
    			
    		}else tamammi=1;
    		
    		
    		
    		if (tamammi==1){
    			break;
    		}
    		
    	} 
    	
    	
    	
    	
    	return gonder;
    	
    }
    
    
    
    in the main section  i want to fill another array by calling this method.
    
    testViewController	*me=[testViewController new];
    [COLOR="Red"]NSArray *sayilar =[[NSArray alloc]initWithArray:[me myNumbers]];[/COLOR]
    [me release;]
    the red one which is my new array I want to fill it from myNumbers method. However i cant do it.

    Thanks for all helps.
     
  2. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #2
    I think the problem is that you have created a Class method but you are trying to access it as an instance method. What you can try to do is change it to an instance method:

    Code:
    - (NSArray*)myNumbers
    
     
  3. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #3
    Yeah I did it but it does't work again :(
     
  4. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #4
    Can you post both the .h and .m file. At what point does it not work? When you press the button or even when you simulate pressing a button by just calling the method itself?
     
  5. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #5
    Code:
    #import <UIKit/UIKit.h>
    
    @interface SayisalViewController : UIViewController {
    	
    	
    	int b;
    	IBOutlet UILabel	*lblYazdir;
    NSMutableArray *sayilar;
    NSMutableArray *gonder;
    }
    
    @property	(nonatomic,retain) IBOutlet UILabel *lblYazdir;
    @property (nonatomic, retain) NSMutableArray *sayilar;
    @property (nonatomic, retain) NSMutableArray *gonder;
    
    - (IBAction) buttonaBas;
    
    - (NSString*) yaz: (NSString *)str;
    
    -(NSMutableArray*)sayilarim;
    
    @end
    
    
    
    
    
    
    
    #import "SayisalViewController.h"
    
    @implementation SayisalViewController
    int ym;
    
    
    @synthesize lblYazdir;
    
    - (IBAction) buttonaBas{
    
    	srand(time(NULL));
    	int z;
    	NSString *yazilacak =[[NSString alloc] init]; 
    	for (z=0 ; z<8; z++ ){
    		
    		
    		for (b=0 ; b <3; b++ ){
    			SayisalViewController	*me=[SayisalViewController new];
    			
    			NSString *dump= [NSString new];
    			dump=[me yaz:@"*"];
    			yazilacak=[yazilacak stringByAppendingString:dump];
    			
    			
    			
    			[dump release];
    			[me release];
    			 
    			
    			ym++;
    		}
    		
    		ym=0;
    	}
    	[lblYazdir setText:yazilacak];
    	
    	
    
    }
    
    
    
    
    
    
    - (NSString*) yaz:(NSString *)str{
    	int i;
    	NSString *yazdir= [[NSString alloc]init];
    	
    	
    	if (ym == 0)
    	{
    		for (i=0; i < 21; i++){
    			yazdir= [yazdir stringByAppendingString:str];
    		}
    		
    	}
    	else if (ym == 1)
    	{
    		
    		//SayisalViewController	*me=[SayisalViewController new];
    		//int *sayilar;
    		
    		NSMutableArray *sayilar =[[NSMutableArray alloc]init];
    		[sayilar addObjectsFromArray:[self sayilarim]];
    		
    
    		
    		//sayilar = [me sayilarim];
    		//[me release];
    		for (i=0; i < 21; i++){
    			if (i==0 || i==8){
    				yazdir= [yazdir stringByAppendingString:str];
    			}else if (i==2)
    			{
    				int j;	  
    				
    				
    				for (j=0; j < 6; j++){
    					int rd; 
    					//rd=sayilar[j];
    					rd=[sayilar objectAtIndex:j];
    					
    					if (rd == 1 || rd==2 || rd==3 || rd==4 || rd==5 || rd==6 || rd==7 || rd==8 || rd==9){        
    						yazdir= [yazdir stringByAppendingString:(@" %d ",rd)];
    						
    					}
    					else
    						yazdir= [yazdir stringByAppendingString:(@"%d ",rd)];
    
    				}
    				
    				
    			}else if (i==3 || i==4 || i==5 || i==6 || i==7 )
    			{
    				
    			}
    			
    			
    			else 
    				yazdir= [yazdir stringByAppendingString:@" "];
    
    			
    		}
    		
    		
    		
    	} else if (ym == 2){
    		
    		for (i=0; i < 21; i++){
    			yazdir= [yazdir stringByAppendingString:str];
    
    		}
    	}
    	
    	yazdir= [yazdir stringByAppendingString:@"\n"];
    
    	
    	return yazdir;
    }
    
    
    -(NSMutableArray*)sayilarim{
    	
    	int tamammi=0;
    	
    	
    	//static int gonder[6];
    	NSMutableArray *gonder = [[NSMutableArray alloc]init];
    	int sayac=0;	
    	while(1)
    	{
    		
    		if (sayac < 6){
    			int rnd=rand()%49+1;
    			
    			int z;
    			int varmi=0;
    			
    			for (z=0;z<6;z++){	
    				
    				[COLOR="Red"]int *bumu =[gonder objectAtIndex:z];[/COLOR]
    				if (bumu==rnd){
    					
    					varmi=1;
    					break;
    				}
    			}
    			
    			if (varmi==0){
    				[gonder addObject:[NSNumber numberWithInt:rnd]];
    								
    				//gonder[sayac]=rnd;						
    				sayac += 1;
    			}
    			
    			
    			
    		}else tamammi=1;
    		
    		
    		
    		if (tamammi==1){
    			break;
    		}
    		
    	} 
    	
    	
    	
    	
    	return gonder;
    	
    }
    
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    When My code comes to Red line i get this at the debug mode GDB:program received signal: "SIGABRT"

    Then it stop after debug step.

    I'm really new, i have been writing with Objective C just 2 or 3 days.
    I am .Net C# VB.Net Programmer, However i wanna learn iphone programming.

    Thank you for replay.
     
  6. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #6
    It looks like you are trying to stuff an NSNumber* into an int*, This will never work... NSNumber is a class and int is a c-type, and they are different.
     
  7. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #7
    it also looks like gonder is empty. it was just initialized a few lines preceding that call to access an objectAtIndex:z. You would also want to test the length of the gonder object if it has anything on it. There are also a few things that you will need to tweak in your code. the IBAction needs an (id)sender as a parameter. Not sure if you can use new to init an object in objective-c. I will take a look at the rest in a bit.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    What book or tutorial are you reading? Be specific.

    If you're not reading a book or tutorial, you probably should be. There are stickies at the top of the forum list with suggestions on books and tutorials.

    In my opinion, you don't know the basics of Objective-C well enough to be trying to write a program of this complexity. You need to go back and revisit the fundamentals.


    At the point where you have this failing code:
    Code:
    int *bumu =[gonder objectAtIndex:z];
    
    Exactly how many objects will there be in the NSMutableArray* gonder?

    You should be able to answer this question simply by looking at your code and walking it through. You should also be able to confirm it using [gonder count] to tell you the actual length at that point.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Actually, it's not necessary; just very common.
     
  10. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #10
    ahhhh.... cool... i didn't know that... thanks for the tip dejo :) im still learning a lot of things myself so I go to the forums too.
     
  11. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #11
    First of all thanks for everyone,

    yes ritsard gonder sends me back 0 object i know, because i create it a few rows below with NSMutableArray *gonder = [[NSMutableArray alloc]init]; however in C it works when i create a new array' its whole objects takes null or 0 value. In that case i try to add new object in it but it gives me the same Errors. For int arrays what should i use NSArray or NSMutableArray or another thing.
     
  12. EnigMoiD macrumors member

    Joined:
    Aug 10, 2007
    #12
    There are no int arrays in objective C; you create an NSArray (or an NSMutableArray if you want to change its contents after you create it) and then fill it with whatever you want using addObject: or insertObject: atIndex:. You can only add objects to an array, so you need to use an NSNumber to store ints with code like
    Code:
    NSNumber * theNumber=[NSNumber numberWithInt:theInt];
    You can then retrieve the int from the object with the intValue method.

    Also, code like
    Code:
    int *bumu =[gonder objectAtIndex:z];
    fails because an int is a primitive and does not require a *; the * signifies a pointer (common with objects). Also, since the objectAtIndex:z is an object, you would probably have to use the intValue method on it to get something the int could hold.
     
  13. maymunaga thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #13
    Thanks For Everyone This is really good Forum..:cool:
     

Share This Page