Emil
Emil

Reputation: 7256

Why does this crash: stringByAppendingFormat

My code crashes at this function (at the stringByAppendingFormat: with error objc_msgSend() selector name: stringByAppendingFormat).

This is that line:

    // imagesPath = ...iPhone Simulator/4.0/Applications/NUMBERS/Documents/images
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingFormat:@"/%d.png", [[self.postsArrayID objectAtIndex:row] intValue]]];

Could it have something to do with the retaining of objects?

Thanks :)

Upvotes: 1

Views: 1482

Answers (3)

Eiko
Eiko

Reputation: 25632

> rootPath =
> [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
> NSUserDomainMask, YES)
> objectAtIndex:0]; imagesPath =
> [rootPath
> stringByAppendingPathComponent:@"/images/"];

Hah! Setting a property and setting value using self.imagesPath =... fixed it. Obj-c is so hard to understand sometimes...

The methods you used to set the get the paths are autoreleased, so when you tried to access them later they had already died. Using the self.imagesPath property will retain the data (you specified it as (nonatomic, retain) - so it will stay around until you release it (or assign anything else using the property accessor self.imagesPath = ....;

Apple's memory management guide is highly recommended, though it is still easy to fail after reading it a couple of times. :-)

Upvotes: 1

jrtc27
jrtc27

Reputation: 8526

Why not use stringByAppendingPathComponent:? And surely imagesPath is not ........../NUMBERS/images? Would it not be ................/<random ID>/images?

Upvotes: 1

fbrereto
fbrereto

Reputation: 35925

Usually a crash in objc_msgSend() implies the message being passed to the object (in this case, stringByAppendingFormat) is not specified for that object. Quick googling reveals that many top pages for stringByAppendingFormat are quite dated, inferring the API has possibly been deprecated in favor of something else.

As a workaround, it would seem +[NSString stringWithFormat:] would be a viable alternative for your use case.

Upvotes: 1

Related Questions