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 08-10-2010, 11:02 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Michigan, United States
Posts: 132
natronp is on a distinguished road
Default dynamically created pointer or 'instance name'

In obj. c can you create a pointer by combining a string with a variable value?

something like so:

Code:
NSMutableArray["myArray"+i] = [[NSMutableArray alloc]init];
where 'i' is a loop variable/int?
natronp is offline   Reply With Quote
Old 08-10-2010, 11:06 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Long Beach, CA
Posts: 612
bytor99999 is on a distinguished road
Send a message via AIM to bytor99999 Send a message via Yahoo to bytor99999
Default

Be a bit more specific please.

This might be something similar to what you are looking for


Code:
NSError *error = nil;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
Where that error variable is used with &error. It might help.

Mark
__________________
Perfect World Programming LLC
http://www.perfectworldprogramming.com

Please check out my apps.

TubeOrganizer
http://www.spritzlerapps.com/tube-organizer.html

Paper Clips
http://spritzlerapps.weebly.com/paper-clips.html
bytor99999 is offline   Reply With Quote
Old 08-10-2010, 11:49 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Michigan, United States
Posts: 132
natronp is on a distinguished road
Default dynamic pointer/name

i want to create an array called "myArray5"

where "5" is added to the string "myArray" and represents the value of an int variable.
natronp is offline   Reply With Quote
Old 08-10-2010, 12:08 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by natronp View Post
i want to create an array called "myArray5"

where "5" is added to the string "myArray" and represents the value of an int variable.
No, Objective-C variable names must be explicitly known at compile time. You cannot generate variable names at run time. Anyway, why do you care what the variable name is? The variable name is just a convenience for the programmer. It has no effect on the user's experience of your app.
RLScott is offline   Reply With Quote
Old 08-10-2010, 12:20 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Michigan, United States
Posts: 132
natronp is on a distinguished road
Default

Quote:
Anyway, why do you care what the variable name is?
I want to separate one large array of objs into separate arrays of 16 objs each.

so in my loop i do something like this:

Code:
if( i%16 == 0)
		{
		//we have a multiple of 16?
			[self galleryArray] addObject: currArray];  push array filled by previous iterations of loop into another array
			[currArray release]; //let go of it, the other array is now owner
			NSMutableArray *currArray = [[NSMutableArray alloc]init]; //create a new array to start filling in next iteration of loop
			
			
			
		}
so that on every sixteenth item in the array, i push the current array into a third array which i will use later. I was hoping that by releasing the 'currArray' first, it would only be retained by the 'galleryArray' and i could alloc/init another 'currArray' and start filling it. This is not the case however as the way i'm doing it seems to push everything into the first 'instance' of 'currArray'.

Ultimately i don't care what the array is called, i'm just using the array temporarily before i push it as an object in another array. I only care in that it seems that using the same pointer results in everything being pushed into the same array...

it would be very convenient to be able to temporarily refer to these temporary arrays by the loop iteration variable, do stuff with them and then get rid of them...

of course i'm assuming there is a better way of doing this in obj c that in my noobness, i'm unaware of!

Last edited by natronp; 08-10-2010 at 12:24 PM.
natronp is offline   Reply With Quote
Old 08-10-2010, 12:30 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Switzerland
Age: 26
Posts: 35
Shod is on a distinguished road
Default

Quote:
Originally Posted by natronp View Post
I want to separate one large array of objs into separate arrays of 16 objs each.

so in my loop i do something like this:

Code:
if( i%16 == 0)
		{
		//we have a multiple of 16?
			[self galleryArray] addObject: currArray];  push array filled by previous iterations of loop into another array
			[currArray release]; //let go of it, the other array is now owner
			NSMutableArray *currArray = [[NSMutableArray alloc]init]; //create a new array to start filling in next iteration of loop
			
			
			
		}
so that on every sixteenth item in the array, i push the current array into a third array which i will use later. I was hoping that by releasing the 'currArray' first, it would only be retained by the 'galleryArray' and i could alloc/init another 'currArray' and start filling it. This is not the case however as the way i'm doing it seems to push everything into the first 'instance' of 'currArray'.
You do not achieve that because you are defining a new variable called currArray inside the if block and as soon as that block ends (the line after) that variable is out of scope. So try and replace:

Code:
NSMutableArray *currArray = [[NSMutableArray alloc]init];
with:

Code:
currArray = [[NSMutableArray alloc]init];
or you could just do:

Code:
[currArray removeAllObjects];
without releasing currArray before ofc.

Last edited by Shod; 08-10-2010 at 12:44 PM.
Shod is offline   Reply With Quote
Old 08-10-2010, 12:54 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

out of date reply, better answered above ^

Last edited by nobre84; 08-10-2010 at 12:56 PM.
nobre84 is offline   Reply With Quote
Old 08-10-2010, 12:55 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Michigan, United States
Posts: 132
natronp is on a distinguished road
Default scope

man, i had no idea that an if statement had its own scope. thanks for the info.

so now i have:

Code:
for(int i=0; i < self.pics.count; ++i)
	{
		
		
		tempObj = [[ItemPic alloc]init];
		tempObj = [[self pics] objectAtIndex:i];
		
		
		[currArray addObject:tempObj]; //add to current array
		
		[tempObj release];
		tempObj=nil;
		
		if( i%16 == 0 )
		{
			NSLog(@"we have multiple of 16");
			//we have a multiple of 16?
			[[self galleryArray] addObject: currArray];
			[currArray release];
			//currArray = nil;
			currArray = [[NSMutableArray alloc]init];
			
		}
		
		
		if(i==self.pics.count-1)
		{
			NSLog(@"end of loop!");
			NSLog(@"GalleryArray count: %i", self.galleryArray.count);
			//we're done paging results into array(s)
			[self loadPage:1];
		}
		
					}
but my:
Code:
if( i%16 == 0 )
		{
must be triggering on the first iteration ( 0/16=0 ) which is not what i want. It's splitting the 23 or so objects in my master array into two arrays, one with one object and one with 16. I realize this is a whole "nother" issue, but what can i do to clean this up?
natronp is offline   Reply With Quote
Old 08-10-2010, 01:36 PM   #9 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Switzerland
Age: 26
Posts: 35
Shod is on a distinguished road
Default

well there are many solutions:

you could change the if statement like this:

Code:
if ((i % 16 == 0) && i) { ... }
since in the first iteration i is equal to 0, the if will evaluate to false.

Or you could check the size of currArray instead

Code:
if (currArray.count == 16) { ... }
and I'm sure there are many other ways.

By the way I just noticed these 2 lines on your code:

Code:
tempObj = [[ItemPic alloc]init];
tempObj = [[self pics] objectAtIndex:i];
What are you doing here is bad, you are leaking memory, because you are allocating an ItemPic object in the first line, and in the second line you are assigning tempObj to another object, losing the reference to the ItemPic object created on the line before, thus you will never be able to release it. So just remove the first line, since it is completely useless. And after that remove also those lines:

Code:
[tempObj release];
tempObj=nil;
Since you have not allocated tempObj, you do not have to release it.

Last edited by Shod; 08-10-2010 at 01:55 PM.
Shod is offline   Reply With Quote
Old 08-10-2010, 02:56 PM   #10 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Michigan, United States
Posts: 132
natronp is on a distinguished road
Default error

nevermind. i think i found the problem with breakpoints and it's outside my loop. Thanks for the suggestion to use the array count vs. modulus!

Last edited by natronp; 08-10-2010 at 03:03 PM.
natronp 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: 348
9 members and 339 guests
bignoggins, Chickenrig, firecall, iNet, linkmx, michaelhansen, Objective Zero, PlutoPrime, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,893
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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