- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的问题。这是我的头文件:
#import <UIKit/UIKit.h>
@interface FirstFaceController : UIViewController
@property (nonatomic,retain) NSMutableDictionary *face1Layers;
@end
这个.m,我在这里初始化我的字典并将其放在 UIImageView 的位置:
#import "FirstFaceController.h"
@implementation FirstFaceController
@synthesize face1Layers;
-(void) dealloc {
[face1Layers release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.face1Layers = [NSMutableDictionary dictionary];
[self.face1Layers setObject:
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.png"]]
forKey:@"pic"];
[self.view addSubview:[self.face1Layers objectForKey:@"pic"]];
if ( [[face1Layers objectForKey:@"pic"] superview] == nil ) {
//....
}
}
然后我调用 [[face1Layers objectForKey:@"pic"]
superview] 我有“EXC_BAD_ACCESS”。为什么?
最佳答案
尝试这样做:
NSMutableDictionary* tempDict = [[NSMutableDictionary alloc] init];
self.face1Layers = tempDict;
UIImageView* picView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.png"]];
[self.face1Layers setObject:picView forKey:@"pic"];
[picView release];
[tempDict release];
不要通过一行代码创建和插入您的 NSMutableDictionary
和 UIImageView
,因为存在泄漏。
在第一种情况下,如果执行以下操作,则保留计数为 2。 face1Layers 有保留策略。
self.face1Layers = [[NSMutableDictionary alloc] init];
您可以避免像我之前解释的那样拆分代码,或者向初始化的对象发送一条 autorelease
消息。
在第二种情况下,当您在 NSDictionary
或 NSArray
(及其子类)中添加对象时,这些类会保留添加的对象。
希望对您有所帮助。
关于iphone - 将 UIImageView 存储在 NSMutableDictionary 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835634/
设置 我有一个 NSMutableDictionary,其中包含超过 800 个代表员工的 NSMutableDictionaries。我正在尝试实现一个搜索栏,但在使用我的词典时遇到严重问题。 在第
我有一个 NSMutableDictionary,其中的每个元素都是另一个字典。将其内容复制到另一个 NSMutableDictionary 的最佳方式是什么?我试过使用: firstDictiona
我有 2 个 NSMutableDictionaries,每个都有 5 个 NSArray, 这 2 个 NSMutableDictionaries 有完全相同的 5 个键和 NSArray,但内容不
将 NSMutableDictionary 添加到 NSMutableDictionary 是创建副本还是保留指针。 我希望能够更新第一个 NSMutableDictionary 的内容并在另一个 N
所以我有三个像这样的 NSMutableDictionary: .h文件 NSMutableDictionary *myContainer; NSMutableDictionary *myD1; NS
我想将 NSMUtableDictionary 值复制到另一个 NSMutableDictionary 中。我怎样才能做到这一点? 这是我的代码, PersonClass.h file: NSMu
仍在此处学习 Objective-C/iPhone SDK。我想我知道为什么这不起作用,但我只是想确认一下。 在awakeFromNib中,如果我使用[[NSMutableDictionary all
我在这个数组中有一个 NSArray 和 NSMutableDictionary 对象,如下所示。我想在这个数组的索引:2 处得到一个对象,并将来自以下数组的 NSMutableDictionary
要创建一个空字典,我应该调用 [NSMutableDictionary dictionary] 还是 [[NSMutableDictionary alloc] init]? 有什么区别? 最佳答案 [
对于存储在 NSMutableDictionary 中的字母表中的每个字母,我有 5 个 UIImageView。 5 而不是 1,以防万一他们想要拼写的单词需要更多相同的字母。 我正在创建这样的字母
一旦 NSMutableDictionary 已经达到对象的容量限制,当您尝试添加到它时会发生什么?这会撞出一个还是会抛出异常?当我添加一个新的字典对象时,有没有办法让它撞出最旧的字典对象? 最佳答案
我有 NSMutableDictionary *mutDic; 它加载了来自其他NSMutableDictionary的一些值从警报中我试图更新其值 - (void)alertView:(UIAler
在我的 iPhone 应用程序中,我使用整数来跟踪大量变量。我在我的 AppDelegate 文件(它是一个多 View 应用程序)中声明并初始化了它们,然后如果我在其他 View (类)中声明它们,
我有一个递归函数,可以将以前的字典添加到新字典中,但使用 addEntriesFromDictionary 会出现错误 这是我的示例代码(已编辑) func fetchAllSWApiList
我有一个应用程序可以从 Web 服务下载信息并将其缓存在内存中。具体来说,我的单例缓存类包含一个实例变量 NSMutableDictionary *memoryDirectory,它包含所有缓存数据。
我今天遇到了一个策略错误。我有一个 NSArray 和一个 NSMutableDictionary。问题是我的 NSMutableDictionary 变成了一个数组。这是代码: 标题: NSArra
我的 iOS 应用程序中有以下方法。声明: GMSMarker *marker = [places objectForKey:place]; 并不总能找到关键的对象,当我明确知道有一个对象要找到时。
我有两个 NSMutableDictionay,我们将预期的一个称为“A”,将操作结果的一个称为“B” 我想将“A”和“B”之间的差异插入“C”NSMutableDictionary。 我使用 if
我有一个日期选择器和一个 UIBarButtonItem,当按下 UIBarButtonItem 时,它会调用一个方法将所选日期添加到 NSMutableDictionary。代码如下所示: diar
我在 NSMutableDictionary 中面临非常奇怪的问题。请看下面的代码。 NSMutableDictionary *dict=[[NSMutableDictionary alloc]in
我是一名优秀的程序员,十分优秀!