Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-28-2009, 06:30 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
NeoNate is on a distinguished road
Question invalid receiver type 'UIImageView *[50]'

I need some help understanding the following error and how to fix it. I have set up the following to handle a series of images:

Code:
UIImageView *Object[50];
I then try to execute the following:

Code:
UIImageView *imageView = [Object objectAtIndex:0];
imageView.layer.zPosition = 1000;
Which gives me a warning about invalid receiver type 'UIImageView *[50]', the application crashes when this line is executed.

What does this mean?

Also do I need to release the memory from imageView at the end?

Many thanks, Nathan
NeoNate is offline   Reply With Quote
Old 06-28-2009, 10:16 AM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

You declared the type of Object as a C array, but then you're trying to access it as an NSArray item. Pick one or the other. Then you're going to have to actually alloc/init the UIImageView objects you're trying to store in the array.

joe
FlyingDiver is offline   Reply With Quote
Old 06-28-2009, 10:51 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
NeoNate is on a distinguished road
Default

Joe, thanks for the reply.

I'm really struggling with this. I don't suppose you could give me some pointers, I'm new to both C and Objective C. Here is what I've been trying to do since my last post:

Code:
NSMutableArray *Object[50];

@property (nonatomic, retain) NSMutableArray *Object;

@dynamic Object;

Object = [[NSMutableArray alloc] init];

for(int i = 0; i < 50; i++)
{
	UIImageView *myImageView = UIImageView * myImageView = [self newPieceViewWithImageNamed:@"" atPostion:CGPointMake(0,0)];[self addSubview:Object[i]];
	[Object addObject:myImageView];
	[myImageView release];
}

UIImageView *imageView = [Object objectAtIndex:0];
imageView.layer.zPosition = 1000;
I still get the same error so imagine I am still mixing the two languages.
NeoNate is offline   Reply With Quote
Old 06-28-2009, 10:55 AM   #4 (permalink)
Tutorial Author
 
Join Date: Jan 2009
Posts: 144
meowmix23F is on a distinguished road
Default

Quote:
Originally Posted by NeoNate View Post
Joe, thanks for the reply.

I'm really struggling with this. I don't suppose you could give me some pointers, I'm new to both C and Objective C. Here is what I've been trying to do since my last post:

Code:
NSMutableArray *Object[50];

@property (nonatomic, retain) NSMutableArray *Object;

@dynamic Object;

Object = [[NSMutableArray alloc] init];

for(int i = 0; i < 50; i++)
{
	UIImageView *myImageView = UIImageView * myImageView = [self newPieceViewWithImageNamed:@"" atPostion:CGPointMake(0,0)];[self addSubview:Object[i]];
	[Object addObject:myImageView];
	[myImageView release];
}

UIImageView *imageView = [Object objectAtIndex:0];
imageView.layer.zPosition = 1000;
I still get the same error so imagine I am still mixing the two languages.
Ok, lets take a look at your code.

You declared *50* NSArrays. Just say

NSMutableArray *object;

NOT

NSMutableArray *object[50];
that means that

object[1] is an NSArray
object[2] is an NSArray
etc.

object itself is not an NSArray, rather a C array with pointers to NSArrays, so when you call alloc init on it it fails because A C ARRAY IS NOT AN OBJECT!
meowmix23F is offline   Reply With Quote
Old 06-28-2009, 11:39 AM   #5 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Try something like this. I just edited your code, it has not been compiled or tested.

Code:
NSMutableArray *viewArray;
@property (nonatomic, retain) NSMutableArray * viewArray;
Code:
@synthesize viewArray;

viewArray = [[NSMutableArray alloc] init];

for(int i = 0; i < 50; i++)
{
	UIImageView *myImageView = [self newPieceViewWithImageNamed:@"" atPostion:CGPointMake(0,0)];
	[viewArray addObject:myImageView];
        [self addSubview: myImageView];
	[myImageView release];
}

UIImageView *imageView = [viewArray objectAtIndex:0];
imageView.layer.zPosition = 1000;
I renamed "Object". Terrible name for a variable.

Create the array. Add all the image views to it. Then use the first one in the array.

You need to check what "newPieceViewWithImageNamed" returns. If it's an auto-released object, you don't need the "[myImageView release];" line, it'll cause a release exception.

joe
FlyingDiver is offline   Reply With Quote
Old 06-28-2009, 11:40 AM   #6 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
NeoNate is on a distinguished road
Talking

Joe and Meowmix23f, thanks that worked brilliantly!

My variables are named better, I was using Object for demonstration, perhaps myArray may have been a better choice

Last edited by NeoNate; 06-28-2009 at 11:54 AM.
NeoNate is offline   Reply With Quote
Old 06-28-2009, 11:53 AM   #7 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Peterborough, England
Posts: 43
NeoNate is on a distinguished road
Thumbs up FINAL SOLUTION

Thanks for Joe and Meomix23f for their help.

For anyone who may refer to this thread in the future I have supplied the full solution below:

Code:
/* HEADER */
NSMutableArray *myArray;

@property (nonatomic, retain) NSMutableArray * myArray;


/* MAIN */
@dynamic myArray;

for(int i = 0; i < 50; i++)
{
	UIImageView * myImageView = [[UIImageView alloc] init];
	[myArray addObject:myImageView];
	[myImageView release];
}


UIImageView *imageView = [myArray objectAtIndex:0];				
imageView = [self newPieceViewWithImageNamed:@"myImage.png" atPostion:CGPointMake(200,400)];	[self addSubview:imageView];	
imageView.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
imageView.layer.zPosition = 100;
				
[imageView release];
NeoNate is offline   Reply With Quote
Old 01-13-2010, 07:11 AM   #8 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 54
Georg is on a distinguished road
Default

Quote:
Originally Posted by NeoNate View Post
Thanks for Joe and Meomix23f for their help.

For anyone who may refer to this thread in the future I have supplied the full solution below:

Code:
/* HEADER */
NSMutableArray *myArray;

@property (nonatomic, retain) NSMutableArray * myArray;


/* MAIN */
@dynamic myArray;

for(int i = 0; i < 50; i++)
{
	UIImageView * myImageView = [[UIImageView alloc] init];
	[myArray addObject:myImageView];
	[myImageView release];
}


UIImageView *imageView = [myArray objectAtIndex:0];				
imageView = [self newPieceViewWithImageNamed:@"myImage.png" atPostion:CGPointMake(200,400)];	[self addSubview:imageView];	
imageView.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
imageView.layer.zPosition = 100;
				
[imageView release];


I am using C array like this:

.h:
UIImageView *imgStone[50];



and in .m with my code:
for (int i=0; i<50; i++)
{
imgStone[i] = [[UIImageView alloc] initWithImage:StoneFileName];
imgStone[i] = CGRectMake(x, y);
[self.view addSubview:imgStone[i]];
[imgStone[i] release];
}


but it is strange, since I've released the imgStone array, I still can move these imgStone by CGPointMake:
imgStone[i].center = CGPointMake(new_x, new_y, width, height);


Just curious if this is correct way? because my app runs quite smooth, but I found out that there is memory leak. If this is not a proper way, then how should I do? I have created another version which is using:

.h:
NSMutableArray *imgStoneArray;
UIImageView *imgStone;

.m:
for(int i=0;i<50;i++)
{
imgStone = [[UIImageView alloc] initWithImage:StoneFileName];
imgStone.frame = CGRectMake(x, y, width, height);
[imgFishArray addObject:imgFStone];
[self.view addSubview:imgStone];
[imgFish release];
}
then move Stones:
for(int i=0;i<50;i++)
{
UIImageView *loadedImage = [[UIImageView alloc] init];
loadedImage = [imgStoneArray objectAtIndex:i];
loadedImage.center = CGPointMake(new_x, new_y);
[loadedImage release];
}

the imgStone moves by a timer (0.2 second)

but the latter one crashed, I don't know what was wrong. Can anyone help on this? which way is better?
Georg is offline   Reply With Quote
Old 01-13-2010, 09:21 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Quote:
Originally Posted by Georg View Post
I am using C array like this:
and in .m with my code:
for (int i=0; i<50; i++)
{
imgStone[i] = [[UIImageView alloc] initWithImage:StoneFileName];
imgStone[i] = CGRectMake(x, y);
[self.view addSubview:imgStone[i]];
[imgStone[i] release];
}

...


m:
then move Stones:
for(int i=0;i<50;i++)
{
UIImageView *loadedImage = [[UIImageView alloc] init];
loadedImage = [imgStoneArray objectAtIndex:i];
loadedImage.center = CGPointMake(new_x, new_y);
[loadedImage release];
}

the imgStone moves by a timer (0.2 second)

but the latter one crashed, I don't know what was wrong. Can anyone help on this? which way is better?
Your first snippet looks ok... You may be leaking when disposing of your array or when moving the images.

In the second snippet you just load the images, you don't need to alloc them again. Your loadedImage release is actually releasing the object in the array, that will crash next time you try to use it, while leaking the unused alloc'd item. Just remove the alloc and release lines from there.
nobre84 is offline   Reply With Quote
Old 01-13-2010, 09:15 PM   #10 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 54
Georg is on a distinguished road
Default

Quote:
Originally Posted by nobre84 View Post
Your first snippet looks ok... You may be leaking when disposing of your array or when moving the images.

In the second snippet you just load the images, you don't need to alloc them again. Your loadedImage release is actually releasing the object in the array, that will crash next time you try to use it, while leaking the unused alloc'd item. Just remove the alloc and release lines from there.

hihi nobre84, I really appreciate your comment, it had solved the problem!! let me know your App name in AppStore.

Another little problem I have is, the dealloc function is never get called, therefore I cannot excute again the subview by pressing the button on mainView:

- (IBAction)btnNewGame{

GameLevel = 1;


if (self.playGameView == nil) {
PlayGameViewController *playGameViewView = [[PlayGameViewController alloc] initWithNibName:@"PlayGameViewController" bundle:nil];
self.playGameView = playGameViewView;
[self.view insertSubviewlayGameViewView.view atIndex:20];
[self.view addSubviewlayGameViewView.view];
//[self.view removeFromSuperview];
[playGameViewView release];
}
NSLog(@"Testing ret count: %d", [self.view retainCount]);
}

which part I was doing wrong?
Georg is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
9 members and 320 guests
chiataytuday, coolman, givensur, ipodphone, jbro, mtl_tech_guy, Punkjumper, vilisei, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:15 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0