我的代码在此函数处崩溃(在stringByAppendingFormat:处出现错误objc_msgSend() selector name: stringByAppendingFormat
)。
这行是:
// imagesPath = ...iPhone Simulator/4.0/Applications/NUMBERS/Documents/images
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingFormat:@"/%d.png", [[self.postsArrayID objectAtIndex:row] intValue]]];
它与物体的保留有关吗?
谢谢 :)
最佳答案
> 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...
用来设置获取路径的方法会自动释放,因此,当您稍后尝试访问它们时,它们已经死亡。使用self.imagesPath属性将保留数据(您将其指定为(非原子性,保留))-因此它将一直存在直到您释放它为止(或使用属性访问器self.imagesPath = ....进行其他分配)。
强烈建议使用Apple的内存管理指南,尽管经过几次阅读后仍然很容易出错。 :-)
关于iphone - 为什么会崩溃:stringByAppendingFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2951508/