Need help on for loops inside initwithobject.

Discussion in 'iOS Programming' started by DAMNiatx, May 20, 2010.

  1. DAMNiatx macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #1
    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
     
  2. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #2
    No, Objective-C does not support lambdas. Create an NSMutableArray then do your for loop and pass that variable as your NSArray parameter.
     
  3. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #3
    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];
    
     
  4. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #4
    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 ?
     
  5. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #5
    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.
     
  6. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #6
    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];			
    
    
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Break your code into smaller pieces. It's easier to manage and debug that way.
     
  8. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #8
    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 :(
    [​IMG]
     
  9. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #9
    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.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    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.
     
  11. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #11
    Then make it bigger.

    Only one bracket or parenthesis on any line, all by themselves.

    Count them up.

    Exactly twice.
     
  12. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #12
    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
     
  13. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #13
    Its always end up with this error

    Code:
     error: called object 'photos' is not a function
    
    any hints ?
    :confused:
     
  14. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #14
    There is too much differing code in prior posts. Can you give the code for context?
     
  15. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #15
    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
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    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.
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    As mentioned earlier, you have errant nils.
     
  18. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #18
    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.

    :)
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    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?
     
  20. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #20
    also no idea man, its have another error if i remove the nil
    [​IMG]
     
  21. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #21
    I think you are worrying about the wrong nil.
     
  22. DAMNiatx thread starter macrumors 6502a

    DAMNiatx

    Joined:
    Jan 20, 2009
    #22
    whats nil are you referring too ?
     
  23. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #23
    See my earlier post. Hint: There's only one nil in that post and it's in red.
     
  24. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #24
    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.
     
  25. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #25
    I think I found them here: MockPhotoSource Class Reference.

    But, to me, the OP seems unable to even grasp the syntax of putting together a value for a parameter.
     

Share This Page