Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

todizara

macrumors newbie
Original poster
Dec 17, 2010
29
0
Hi everyone!
I'd like to display 16 button with a title of number between 1 and 8. The titles will be assigned by chance, but a number will appear only twice.

My code is as follows. When I compile it, while it is blank on the simulator.

A line (on the r = r-1) advised me : assignment makes integer from pointer without a cast
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	int myRand, a, r;
	a=0; //initialize a
	NSArray * test = [[NSArray alloc] init]; //Table for testing the random
	for (int i=0; i<=3; i++){
		for (int j=1; j<=4; j++){
			r=2;
			while (r!=0){
				myRand = 1+arc4random() % 9;
				for(int k=0; k=a; k++){
					if(myRand=[test objectAtIndex:k]){
						r=r-1;
					}
				}
				UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
				myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
				[myButton setTitle:[NSString stringWithFormat: @"%d",myRand] forState:UIControlStateNormal];
				myButton.tag =myRand;
				[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
				[self.view addSubview:myButton];
				r=1;
			}
		}
	}
	
}
I hope as i was clear, thank you to be good enough help me.
 
The line with the error is:
Code:
if(myRand=[test objectAtIndex:k]){

What type is myRand? What type does objectAtIndex: return? Are they the same?
 
A few odd comments:

You create an immutable NSArray, don't add anything to it when you create it, and then call objectAtIndex on that array. Can't work.

Do you mean this:

Code:
if(myRand == [test objectAtIndex:k]){

Do you mean this:

Code:
for(int k=0; k == a; k++){

If you want to display a set of random numbers you should create a list of the numbers you want to display, then shuffle the list, then just iterate over the list.
 
If you want to display a set of random numbers you should create a list of the numbers you want to display, then shuffle the list, then just iterate over the list.
You're right, I'll do it like that.
Thank you

By the way, how to mix the elements of an array as
NSArray * myList = [NSArray arrayWithObjects: @ "1", @ "2", @ "3", @ "1", @ "4", @ "3", nil];

I know it in C but not in objective-C.
 
Last edited:
You're right, I'll do it like that.
Thank you

By the way, how to mix the elements of an array as
NSArray * myList = [NSArray arrayWithObjects: @ "1", @ "2", @ "3", @ "1", @ "4", @ "3", nil];

I know it in C but not in objective-C.

I arranged the array elements.
I did leave several buttons and display these elements later. The problem is now running the application. He runs around and closes after three seconds.

Here's code.
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	NSMutableArray * myListe = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:6],[NSNumber numberWithInt:7], [NSNumber numberWithInt:7], [NSNumber numberWithInt:8], [NSNumber numberWithInt:8],  nil];
	int nb=[myListe count];
	int ordre[nb];
	int n, number,a;
	while (myListe.count > 0) {
	n = arc4random() % (myListe.count);
	number = [[myListe objectAtIndex:n] intValue];
	ordre[myListe.count-1] = number;
	[myListe removeObjectAtIndex:n];
	}
a=0;
for (int i=0; i<=3; i++){
	for (int j=1; j<=4; j++){
	int value=ordre[a];
	UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
	[myButton setTitle:[NSString stringWithFormat: @"%d",value] forState:UIControlStateNormal];
	myButton.tag =value;
	[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:myButton];
	a++;
	}
}
	[myListe release];	
}
 
This is a valid shuffle algorithm

Code:
for (int i = [aMutableArray count]; i > 1; i--)
    [aMutableArray exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];
 
What basic debugging have you done to determine the cause? Are there compile-time errors or warnings? What about at run-time? Have you looked at the crash log? Etc.

the problem is on the line
int value = order [0] Assigned value IS garbage or undefined
 
I don't think your ordre array is being setup as dynamically as you would like it to be.
what to do, do you have an idea?

This is a valid shuffle algorithm

Code:
for (int i = [aMutableArray count]; i > 1; i--)
    [aMutableArray exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];

I use your code, but the application always crash.
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	NSMutableArray * myListe = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:6],[NSNumber numberWithInt:7], [NSNumber numberWithInt:7], [NSNumber numberWithInt:8], [NSNumber numberWithInt:8],  nil];
for (int i = [myListe count]; i > 1; i--)
    [myListe exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];
int a=0;
for (int i=0; i<=3; i++){
	for (int j=1; j<=4; j++){
	value = [[myListe objectAtIndex:a] intValue];
	UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
	[myButton setTitle:[NSString stringWithFormat: @"%d",value] forState:UIControlStateNormal];
	myButton.tag =value;
	[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:myButton];
	a++;
	}
}
	[myListe release];	
}
 
Last edited by a moderator:
...but the application always crash.
Okay, but again what debugging have you done? Saying it "always crash" is just far too vague for us to really help. The more relevant information you can provide up front, the quicker we can help you. Perhaps take a gander at Mike Ash's excellent Getting Answers blog article.

EDIT:

P.S. The above code won't even compile since value is undeclared.
 
This line is wrong:
Code:
[myListe release];

The rest looks more or less OK.

Regarding debugging:

Turn on Run > Stop At Objective-C exceptions.
Run Build and analyze on your code.

Set a breakpoint at the start of the new code.
Single step through the code and verify that it's doing what you expect it to do.
Tell us what line it crashes on.
What is shown in the debugger console after it crashes?
 
Thank you for your indication.

I have another error on the line for (b = 0, b <= 16 b + +)

Code:
NSString *buttonName = @"button";
for (int b=1; b<=16; b++){
	NSString *num = [NSString stringWithFormat:@"%d",b];
	NSString *myButton = [NSString stringWithFormat:@"%@%@",buttonName,num];
	UIButton *myButton;
}

l'erreur est expected identifier or '(' before 'for'
 
Last edited:
Well, there are a number of issues with that code snippet, but none of them is related to your error. You should probably give us the code for your entire method.
 
Well, there are a number of issues with that code snippet, but none of them is related to your error. You should probably give us the code for your entire method.

Here is my codes
Code:
#import "MemoViewController.h"

@implementation MemoViewController

NSString *buttonName = @"button";
int b=0;
for (int k=0; K<16; k++){
	NSString *num = [NSString stringWithFormat:@"%d",b];
	NSString *myButton = [NSString stringWithFormat:@"%@%@",buttonName,num];
	UIButton *myButton;
	b++;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	NSMutableArray * myListe = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:6],[NSNumber numberWithInt:7], [NSNumber numberWithInt:7], [NSNumber numberWithInt:8], [NSNumber numberWithInt:8],  nil];
	for (int i = [myListe count]; i > 1; i--)
		[myListe exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];
	int a=0;
	for (int i=0; i<=3; i++){
		for (int j=1; j<=4; j++){
			int value = [[myListe objectAtIndex:a] intValue];
			myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
			myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
			//[myButton setTitle:[NSString stringWithFormat: @"%d",value] forState:UIControlStateNormal];
			myButton.tag =value;
			[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
			[self.view addSubview:myButton];
			a++;
		}
	}
	//[myListe release];	
}

int tag;

-(IBAction) buttonClicked:(id)sender{
	tag = [sender tag] ;
	[self ChangeButton];
}

-(void) ChangeButton{
	[myButton setTitle:[NSString stringWithFormat: @"%d",tag] forState:UIControlStateNormal];
}

- (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)dealloc {
    [super dealloc];
}

@end
There are also error on the line where there is myButton saying they are not declared before, but I believe that this problem will be solved if the problem of the loop for is resolved.
 
Here is my codes
Code:
#import "MemoViewController.h"

@implementation MemoViewController

[COLOR="Red"]NSString *buttonName = @"button";
int b=0;
for (int k=0; K<16; k++){
	NSString *num = [NSString stringWithFormat:@"%d",b];
	NSString *myButton = [NSString stringWithFormat:@"%@%@",buttonName,num];
	UIButton *myButton;
	b++;
}
[/COLOR]
- (void)viewDidLoad {
    [super viewDidLoad];
	NSMutableArray * myListe = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:6],[NSNumber numberWithInt:7], [NSNumber numberWithInt:7], [NSNumber numberWithInt:8], [NSNumber numberWithInt:8],  nil];
	for (int i = [myListe count]; i > 1; i--)
		[myListe exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];
	int a=0;
	for (int i=0; i<=3; i++){
		for (int j=1; j<=4; j++){
			int value = [[myListe objectAtIndex:a] intValue];
			myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
			myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
			//[myButton setTitle:[NSString stringWithFormat: @"%d",value] forState:UIControlStateNormal];
			myButton.tag =value;
			[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
			[self.view addSubview:myButton];
			a++;
		}
	}
	//[myListe release];	
}

[COLOR="red"]int tag;[/COLOR]

-(IBAction) buttonClicked:(id)sender{
	tag = [sender tag] ;
	[self ChangeButton];
}

-(void) ChangeButton{
	[myButton setTitle:[NSString stringWithFormat: @"%d",tag] forState:UIControlStateNormal];
}

- (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)dealloc {
    [super dealloc];
}

@end
There are also error on the line where there is myButton saying they are not declared before, but I believe that this problem will be solved if the problem of the loop for is resolved.

Code like which I've highlighted in red must exist within a method. It makes no sense outside of one.
 
Code like which I've highlighted in red must exist within a method. It makes no sense outside of one.

Do you have an idea to declared 16 buttons myButton1, myButton2, ..., myButton16. To avoid rewriting UIButton myButton1, ..., UIButoon myButton16
 
Do you have an idea to declared 16 buttons myButton1, myButton2, ..., myButton16. To avoid rewriting UIButton myButton1, ..., UIButoon myButton16

Yeah, an array would probably be the best solution for that.

But, I gotta ask: how comfortable are you with the basics of Objective-C / iPhone programming? It seems to me you don't have a good enough understanding of the fundamentals. Perhaps you should consider stepping away from the real coding to go learn those.
 
Yeah, an array would probably be the best solution for that.

But, I gotta ask: how comfortable are you with the basics of Objective-C / iPhone programming? It seems to me you don't have a good enough understanding of the fundamentals. Perhaps you should consider stepping away from the real coding to go learn those.

Thank you for the information, I solve the problem.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.