tags:

views:

185

answers:

1

I am new to this, so please dumb it down for me. I have been tinkering with Objective-C for an iPhone app and have hit a road block.

I am trying to pass the value contained in a variable (string) to a method but it doesn't work. I am guessing it is because the variable is probably a pointer to the value, not the value itself. I wonder if you can suggest how I fix.

Here is the method

- (UIImage *)newUIImageWithURLString:(NSString *)urlString
{
   return [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
}

Sending this message works ... Here I have hard coded the URL, being sent.

UIImage* newimage = [self newUIImageWithURLString:@"http://images.mydomain.com/PhotoServer/Thumb/97/83358897.jpg"];

This one doesn't ... Here the variable is being passed.I get an error that the passed value has no value.

UIImage* newimage = [self newUIImageWithURLString:newListing.ListingPhotoUrl];

I know it does have a value as the following does works.

self.ListingtitleLabel.text = newListing.ListingPhotoUrl;

Any thoughts?

+2  A: 

Are you sure that the dynamic URL is valid? For instance, if newListing.ListingPhotoURL is not a valid URL or cannot be accessed, you will get this error since you are not doing any error checking. While your code is concise, you should probably be just a little bit more verbose while you debug, or at least until you're more comfortable with the language. I would suggest rewriting your method as such:

-(UIImage*)newUIImageWithURLString:(NSString*)urlString {
  NSParameterAssert(urlString);
  NSData *pictureData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
  NSAssert1(pictureData, @"No data returned for url: %@", urlString);
  return [[UIImage alloc] initWithData:pictureData];
}

With the method written like this, you will know right away if something is not behaving as expected (the assertion macros will throw exceptions). This also gives you more granular places to set breakpoints if you're trying to debug something.

Jason Coco