Need help on for loops inside initwithobject.

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
hi guys,

is it possible to have loops inside initwithObject ?


Code:
self.photoSource = [[MockPhotoSource alloc]
		initWithType:MockPhotoSourceNormal
                title:@"Flickr Photos"
	        photos:[[NSArray alloc] initWithObjects:
                   [[[MockPhoto alloc]
                   initWithURL:@"url"
                   size:CGSizeMake(407, 500)] autorelease],
i want to replace mockphoto with multiple values using loops.

Code:
self.photoSource = [[MockPhotoSource alloc]
		initWithType:MockPhotoSourceNormal
                title:@"Flickr Photos"
	        photos:[[NSArray alloc] initWithObjects:
                  
                  for (int y = 0; y < 20; y++) {
                   
                   [[[MockPhoto alloc]
                   initWithURL:@"url"
                   size:CGSizeMake(407, 500)] autorelease],
                   

                    }
any suggestion guys ?

thanks
 

TiberiusXavier

macrumors member
Apr 18, 2010
54
0
Chicago
No, Objective-C does not support lambdas. Create an NSMutableArray then do your for loop and pass that variable as your NSArray parameter.
 

rossipoo

macrumors regular
Jun 7, 2009
109
0
Use initWithObjects:count: or arrayWithObjects:count:

Code:
MockPhoto *photo[20];

for (int i = 0; i < 20; ++i) {
	photo[i] = [[MockPhoto alloc]
		initWithURL:@"url"
		size:CGSizeMake(407, 500)];
	[photo[i] autorelease];
}

NSArray *photos = [NSArray arrayWithObjects:photo count:20];

[self.photoSource = [[MockPhotoSource alloc]
	initWithType:MockPhotoSourceNormal
	title:@"Flickr Photos"
	photos:photos];
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
Use initWithObjects:count: or arrayWithObjects:count:

Code:
snip
hi, thanks for your help

i get an error when trying implement your code.

Code:
called object is not a function
any help for this ?
 

rossipoo

macrumors regular
Jun 7, 2009
109
0
I don't know the exact error, but I think it might be caused by a lone bracket at the start of the line

[self.photosource = ...

remove that bracket and try that.
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
I don't know the exact error, but I think it might be caused by a lone bracket at the start of the line

[self.photosource = ...

remove that bracket and try that.
just tried, and still have bracket problem. :(


Code:
self.photoSource = [[MockPhotoSource alloc]
		initWithType:MockPhotoSourceNormal
		title:@"Flickr Photos"
		photos:[[NSArray alloc] initWithObjects:
//i'm looking to make for loops working inside initWithObject

           for (int y = 0; y < 20; y++) {

			[[[MockPhoto alloc]
			initWithURL:@"url"
			smallURL:@"smallurl"
			size:CGSizeMake(960, 1280)] autorelease],	
                       }


				nil]
			photos2:nil];
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
Break your code into smaller pieces. It's easier to manage and debug that way.
thats all basic code, to make it works, sorry i can't make it more smaller.
Code:
	NSMutableArray *array = [[NSMutableArray alloc] init];
	
	for (int i=0; i< 15; i++)
	{
		//[array addObject:[MyClass constructorWithString:@"String"]];
		[array addObject:[[[MockPhoto alloc]
		   initWithURL:@"url"
		   smallURL:@"Smallurl"
						   size:CGSizeMake(960, 1280)] autorelease]
		 ];
	}
	
	
	self.photoSource = [[MockPhotoSource alloc]
						initWithType:MockPhotoSourceNormal
						title:@"Flickr Photos"
						photos:array,
nil
								]
						photos2:nil
						];
pastebin link http://pastebin.com/ebmrUAib

left with 2 errors :(
 

rossipoo

macrumors regular
Jun 7, 2009
109
0
What is the photos2: argument? And it looks like there is a nil that's just floating around. Did you make the class MockPhotoSource? I've never seen it before, I don't know what it's arguments are, I'm just guessing based on the first snippit of code you posted. Also those errors suggest that there is a problem in that area.

My assumption is that the photos: argument takes an NSArray, so you need to get rid of that nil.

I think it should look like this:
Code:
self.photoSource = [[MockPhotoSource alloc]
	initWithType:MockPhotoSourceNormal
	title:@"Flickr Photos"
	photos:array]; // Note that this object has not been released
I also edited your pastebin link.
 

dejo

Moderator
Staff member
Sep 2, 2004
15,725
447
The Centennial State
thats all basic code, to make it works, sorry i can't make it more smaller.
What I meant is that rather than trying to code the whole thing as a one-liner, break it down into smaller assignments and build from there. But, it seems like you went back and edited your code so my comment looks out of place now.

Anyways, here's an example of breaking code into smaller chunks. Instead of this:
Code:
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.searchengine.com?search=%@", searchKeywords]]];
you could do this:
Code:
NSString *urlString = [NSString stringWithFormat:@"http://www.searchengine.com?search=%@", searchKeywords];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
And, to me at least, it seems you are just throwing code into stuff without understanding how it's meant to be put together. Perhaps it is time to step away from the real coding and go (re)learn the basics of Objective-C.
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
Actually, i'm implementing ttcatalog from three20 Framework. and it impossible to remove photos2 because its included on main class.

thanks for the help guys, i will find a way to make it work.
any suggestion always welcome.
this is the full code
http://pastebin.com/HpD2mPmU
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
Its always end up with this error

Code:
 error: called object 'photos' is not a function
any hints ?
:confused:
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
this is the full code : http://pastebin.com/Ct3mCg7v
Code:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary {
MockPhoto *photo[20];
	
	for (int i = 0; i < 20; ++i) {
		photo[i] = [[[MockPhoto alloc]
					initWithURL:@"url"
					smallURL:@"smallurl"
					size:CGSizeMake(407, 500)] autorelease];	
	}
	NSArray *photos = [NSArray arrayWithObjects:photo count:20];

	self.photoSource = [[MockPhotoSource alloc]
                initWithType:MockPhotoSourceNormal
		title:@"Flickr Photos"
		photos:photos
	     		nil]
					
		photos2:[[NSArray alloc] initWithObjects:
			 [[[MockPhoto alloc]
			 initWithURL:@"url"
			 smallURL:@"smallurl"
		         size:CGSizeMake(800, 533)] autorelease],
		         nil]
			];
}
thanks
 

chown33

Moderator
Staff member
Aug 9, 2009
8,378
4,366
Pumpkindale
this is the full code : http://pastebin.com/Ct3mCg7v
Code:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary {
MockPhoto *photo[20];
	
	for (int i = 0; i < 20; ++i) {
		photo[i] = [[[MockPhoto alloc]
					initWithURL:@"url"
					smallURL:@"smallurl"
					size:CGSizeMake(407, 500)] autorelease];	
	}
	NSArray *photos = [NSArray arrayWithObjects:photo count:20];

	self.photoSource = [[MockPhotoSource alloc]
                initWithType:MockPhotoSourceNormal
		title:@"Flickr Photos"
		photos:photos
	     		[COLOR="Red"]nil[/COLOR]] [COLOR="DarkOrange"]; //missing[/COLOR]
					
		[COLOR="Blue"]photos2:[/COLOR] [COLOR="LimeGreen"][[NSArray alloc] initWithObjects:
			 [[[MockPhoto alloc]
			 initWithURL:@"url"
			 smallURL:@"smallurl"
		         size:CGSizeMake(800, 533)] autorelease],
		         nil]
			];[/COLOR]
}
The red-hilited nil is wrong.

The orange-hilited semicolon was missing.

The blue-hilited "photos2:" will be treated as a label. It's unknown whether this label is intended to be a label or not. My guess is it's not, since nothing ever goes to it. That would make it unnecessary.

The green-hilited statement is either completely unnecessary, or is leaking an array. My guess is it's unnecessary and should be removed.
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
The red-hilited nil is wrong.

The orange-hilited semicolon was missing.

The blue-hilited "photos2:" will be treated as a label. It's unknown whether this label is intended to be a label or not. My guess is it's not, since nothing ever goes to it. That would make it unnecessary.

The green-hilited statement is either completely unnecessary, or is leaking an array. My guess is it's unnecessary and should be removed.
photos2 can't be removed, its attached on delegate class. photos2 have default code without loops, and i need loops in photos, because that i have *array and define photos to an array like rossipoo suggested.

:)
 

dejo

Moderator
Staff member
Sep 2, 2004
15,725
447
The Centennial State
photos2 is the least of your issues right now (although I would set up a variable to contain its value earlier, like what you've started doing with photos).

So, what are you going to do about your unnecessary nil?
 

DAMNiatx

macrumors 6502a
Original poster
Jan 20, 2009
666
17
photos2 is the least of your issues right now (although I would set up a variable to contain its value earlier, like what you've started doing with photos).

So, what are you going to do about your unnecessary nil?
also no idea man, its have another error if i remove the nil
 

chown33

Moderator
Staff member
Aug 9, 2009
8,378
4,366
Pumpkindale
also no idea man, its have another error if i remove the nil
You need to post the code for your MockPhotoSource method initWithType: etc.

We've only been guessing what its parameters are, since you've never shown them.

Based on the error message, I could guess what the problem is, but it's a lot more accurate if we have your real code to look at, rather than imagining what code you might have written, and trying to debug that imaginary code.


You should also get in the habit of finding results by yourself. If I enter google search terms xcode missing sentinel in function call, a number of results appear in which other programmers have seen this message and discovered why it occurs. All I did was enter the exact text of the error message, and precede it with the word xcode to tell google that I only wanted Xcode-related search results. This is one of the simplest of strategies for finding what the cause of an error is, yet it works surprisingly well most of the time.