views:

223

answers:

3

Hey.

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 :)

+1  A: 

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.

fbrereto
[It's still there](http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAppendingFormat:), i don't see it being deprecated.
Georg Fritzsche
stringWithFormat won't work. I want to append a string to another string.
Emil
`UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat: @"%@/%d.png", imagesPath, [[self.postsArrayID objectAtIndex:row] intValue]]]];`
falconcreek
`stringByAppendingFormat` is not listed as a class method of NSString in the documentation. http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-stringWithFormat_
falconcreek
@falconcreek I would say that this code wouldn't work (properly) either as `imagesPath` is still not what OP expects it to be. Of course, this might fix crash or move it somewhere else. And `stringByAppendingFormat` is an instance method as name suggests http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-stringByAppendingFormat_
iPhone beginner
good points. `imagesPath` is likely nil. Only way to tell is if the rest of the code is added to the question. @Emil, set a breakpoint on the line that you posted and check that `imagesPath` is set to what you expect it to be.
falconcreek
No need, I forgot to assign a property and retaining it in h.It's fixed now :)
Emil
+1  A: 

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

jrtc27
I'm using that now. That doesen't matter, does it? I don't set that value, it was just to explain to you how it was built up.
Emil
+1  A: 
> 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. :-)

Eiko
I'll read it. Do you have a link?I've had a couple of these crashes before :p
Emil
Sure, here's the link: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html I think they have an exact copy of it in the iPhone docs.
Eiko