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/Docum开发者_如何学运维ents/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 :)
> 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. :-)
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.
Why not use stringByAppendingPathComponent:
? And surely imagesPath
is not ........../NUMBERS/images
? Would it not be ................/<random ID>/images
?
精彩评论