我有一个奇怪的问题。 pictureLink是.h中声明的全局变量
NSString *pictureLink;
}
@property(retain,nonatomic) NSString *pictureLink;
我写了这段代码
NSString * myPictureUrl=[NSString stringWithFormat:@"http://mywebsite.com/uploads/%@.jpg",hash];
pictureLink=myPictureUrl;
我有一个奇怪的结果,它一定是一个指针 或者
pictureLink=[NSString stringWithFormat:@"http://mywebsite.com/uploads/%@.jpg",hash];
我有 EXC_BAD_ACESS 错误
最佳答案
这是内存管理错误,您没有在代码中保留 myPictureUrl
。
[NSString stringWithFormat:@"http://mywebsite.com/uploads/%@.jpg",hash];
返回一个自动释放的值,因此您有两个选择:
pictureLink=myPictureUrl;
应类似于[self setPictureLink:myPictureUrl];
。- 执行
[myPictureUrl keep];
,并且不要忘记稍后释放
它。
考虑为您的项目使用 ARC(自动保留计数)。使用 ARC,编译器会处理保留计数,因此您不必这样做,实际上不允许这样做。有一个重构可以转换当前项目。
关于iphone - 获取 NSString 变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8505975/