iphone - 为什么会崩溃:stringByAppendingFormat

标签 iphone string crash

我的代码在此函数处崩溃(在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/

相关文章:

iOS App 将通用更改为仅限 iPhone

ios - 关于 Apple 的应用程序登录审查政策

Java - 将字符串转换为 double 的最有效方法

c++ - 需要帮助调试我的反转字符串中单词的代码

iphone - 从App Store下载的应用程序无法启动,没有崩溃或崩溃日志

iphone - 用 Quartz 2D 绘制中文字体字形?

iphone - 使用 Objective c 在后台调用类方法

java - 如何获取空格后面的单词首次出现后的剩余文本?

android - Android应用不会第二次运行

google-chrome - Google Chrome 版本 49 上的 Highcharts Point.update() 崩溃