// The following variables are declared in the header file like this;
UIImageView* block[10][10];
NSString* blockType[3];
//The following variables are declared in the .m like this;
@synthesize island;
-(void)dealloc {
[island dealloc];
blockType[0] = @"1.png";
blockType[1] = @"2.png";
blockType[2] = @"";
1. dealloc should only ever be preceded by the word super.
2. C arrays don't do any memory management like NSArrays do. You are autoreleasing your UIImages. Since the array doesn't keep them alive, they die, and the array continues to point at the dead objects. I'm also guessing that you typed something wrong, and actually mean UIImageView. It is very import to copy-paste the code you are having trouble with.
1. dealloc should only ever be preceded by the word super.
So by this you mean with only [super dealloc] I'll be fine? I won't have to dealloc everything I synthesize one by one? This does it all for me?
Quote:
Originally Posted by BrianSlick
2. C arrays don't do any memory management like NSArrays do. You are autoreleasing your UIImages. Since the array doesn't keep them alive, they die, and the array continues to point at the dead objects. I'm also guessing that you typed something wrong, and actually mean UIImageView. It is very import to copy-paste the code you are having trouble with.
I don't fully understand:
When to release.
The difference between releasing and deallocating.
or when exactly to use autorelease.
I'm a newbie.
Also, I'm pretty sure I didn't mistype it. I am creating a UIImage out of a random image, and assigning it to the block[blockX][blockY] UIImageView. Should it be done another way?
So by this you mean with only [super dealloc] I'll be fine? I won't have to dealloc everything I synthesize one by one? This does it all for me?
You have to release the items that you synthesize. Not dealloc.
Quote:
Originally Posted by UnretroGamer
I don't fully understand:
When to release.
The difference between releasing and deallocating.
or when exactly to use autorelease.
I'm a newbie.
This is clear. Do some research on memory management.
Quote:
Originally Posted by UnretroGamer
Also, I'm pretty sure I didn't mistype it. I am creating a UIImage out of a random image, and assigning it to the block[blockX][blockY] UIImageView. Should it be done another way?
block is defined as a UIImage. UIImages do not have a center property. UIImageViews do. And then you are adding block as a subview, which is not something you can do with a UIImage.
Actually just read the second one. There's no garbage collection in iOS and the Memory Management Guide from the second link covers all the topics you need.
As a quick tip, everything you init, retain or copy, you need to explicitly release.
block is defined as a UIImage. UIImages do not have a center property. UIImageViews do. And then you are adding block as a subview, which is not something you can do with a UIImage.
Alright I'm a bit confused but I updated my code to the following, it still doesn't work but hopefully I fixed what you told me to.
Alright so I updated the code. This code is as simple as it gets. When I execute it using an IBAction like this:
Code:
-(void)startGenerating {
[self generateMap];
}
It doesn't crash, but the Toolbar Item Button I use to execute the IBAction stays darkened after being pressed. Does this mean it's processing, not responding, or do I need to do s'more tweaking?
Again I'm new so don't hesitate to say I'm doing things completely wrong.
I'm hoping that the arc4random() will generate a whole number between 0 and 2. The reason I put it there is because I want the number it generates to be the same for all instances of "randomBlockType" through that loop.
P.S. Turns out the IBAction wasn't executing the code, so I moved the execution to the viewDidLoad but now when I Build and Run I just get a black screen with the status bar.
I'm pretty sure that if you drop a log in there, you'll find that you have created an infinite loop. for loop declarations consist of 3 parts: 1) an initial value, 2) a test to determine if the loop should continue, 3) a modification for each iteration. You have removed #2 and #3. There is nothing to make that loop exit.
At a glance, there is no reason for the 3rd loop to be a loop.
So how would I go about doing what I want to do? Which is for each loop, generate a random block type number, but have the number be the same throughout each loop.
What I want this code to do is loop through 100 UIImageViewss, assign a random image, tag them with a number according to that image, give them a location according to which UIImageView of the array it is, and then add them to the View.
Just declare the variable exactly like you are, but don't put it inside a for statement.
Won't randomBlockType generate a different number every time it is asked for one though? I want it to be a random number each loop, but throughout each loop to be the same number.
You have an X loop, and you have a Y loop. Which one should have a random number for each pass?
You don't seem to be creating image views anymore, which would explain why you aren't seeing anything.
The reason I have an X loop and Y loop is because I want to make 100 ImageViews in total. I separated them so that I can position them on a grid system. block[0][0] should be on the topLeft, block [9][0] - top right, block [0][9] on the bottom left and block [9][9] should end up being on the bottom right.
Here is my block of code with comments to run you through what I designed it to do:
Code:
-(void) generateMap {
for (int blockX = 0; blockX < 10; blockX++){
for (int blockY = 0; blockY < 10; blockY++){
int randomBlockType = arc4random()%2; //generate a random number from 0-2
NSLog(@"%d", randomBlockType); //where's the console window???
UIImage* blockImage = [UIImage imageWithContentsOfFile:blockType[randomBlockType]]; //create a UIImage out of one of three possible images
block[blockX][blockY] = [[UIImageView alloc] initWithImage: blockImage]; //assign the image to one of the UIImageViews I declared in the .h
block[blockX][blockY].tag = randomBlockType; //tag the UIImageView according to the blockType for later use
CGRect blockFrame = block[blockX][blockY].frame; //create a frame
blockFrame.origin = CGPointMake(island.center.x + (128 * blockX), island.center.x + (128 * blockY)); //assign the frame the desired coordinates of this UIImageView
block[blockX][blockY].frame = blockFrame; //assign the UIImageView the properties of the blockFrame I made
[self.view addSubview:block[blockX][blockY]]; //add the UIImageView to the view
}
}
}
-(void) generateMap {
for (int blockX = 0; blockX < 10; blockX++){
for (int blockY = 0; blockY < 10; blockY++){
int randomBlockType = arc4random()%3; //generate a random number from 0-2
NSLog(@"%d", randomBlockType);
UIImage* blockImage = [UIImage imageWithContentsOfFile:blockType[randomBlockType]]; //create a UIImage out of one of three possible images
block[blockX][blockY] = [[UIImageView alloc] initWithImage: blockImage]; //assign the image to one of the UIImageViews I declared in the .h
block[blockX][blockY].tag = randomBlockType; //tag the UIImageView according to the blockType for later use
CGRect blockFrame = block[blockX][blockY].frame; //create a frame
blockFrame.origin = CGPointMake(island.center.x + (128 * blockX), island.center.x + (128 * blockY)); //assign the frame the desired coordinates of this UIImageView
block[blockX][blockY].frame = blockFrame; //assign the UIImageView the properties of the blockFrame I made
[self.view addSubview:block[blockX][blockY]]; //add the UIImageView to the view
NSLog(@"Loop [%d][%d] finished", blockX, blockY);
}
}
}
I'm guessing that could help you guys help me find out whats wrong with the code.
What I see when I build and run the code is just what I created in the Interface Builder, I don't see the 100 UIImageViews. NO crashing though, so that's gotta be good.
Last edited by UnretroGamer; 12-30-2010 at 08:31 PM.
Reason: shortened it s'more