访问 NSDictionary 问题
我只是 iPhone 开发的新手,所以如果这是一个愚蠢的问题,我很抱歉。我正在根据我正在阅读的一本书构建各种应用程序,他们的建议之一是构建一个迷你网络浏览器。我认为这很容易,但尽管大部分都很简单,但我在 NSDictionary 上遇到了很大的困难。
我有一个 UISegmentedControl 用于显示各种书签。 UISegmentedControl
按钮上显示的书签名称将是我的键,而 url 是与其关联的值。
我首先尝试将 NSDictonary
声明为私有(全局变量),但由于我无法让它工作,我只好在头文件中声明它,如下所示:
@property (nonatomic, retain) NSDictionary *bookmarks;
我合成它并初始化它在 viewDidLoad 中如下:
- (void)viewDidLoad
{
bookmarks = [NSDictionary
dictionaryWithObjectsAndKeys:@"http://www.microsoft.com",
@"Microsoft",
@"http://www.google.com",
@"Google",
@"http://www.apple.com",
@"Apple",
@"http://msdn.microsoft.com",
@"MSDN", nil];
[super viewDidLoad];
}
然后,我将一个控件与我的分段控件关联起来,当触发事件并调用该函数时,我将调用以下代码:
- (IBAction) getShortcut:(id)sender
{
NSString *shortcut;
shortcut = [shortcuts titleForSegmentAtIndex:shortcuts.selectedSegmentIndex];
NSString *url = [bookmarks valueForKey:shortcut];
//[self navigateTo: url];
[url release];
}
当 UISegmentedControl 中的按钮时> 是单击,我提取该值并将其存储到快捷方式中,然后尝试使用快捷方式变量作为键从 NSDictionary
“书签”中提取关联的值,但它一直崩溃< /strong> NSString *url = [书签 valueForKey:shortcut];
并炸弹出我的功能并显示常见错误 EXC_BAD_ACCESS
任何帮助将不胜感激。
T。
I'm only new to iPhone development, so my apologies if this is a silly question. I'm building various apps based on a book I'm reading and one of their suggestion was to build a mini web browser. I thought this would be easy, but while most of it is, I'm seriously struggling with the NSDictionary
.
I have a UISegmentedControl
used to display various bookmarks. The bookmark name that is displayed on the buttons of the UISegmentedControl
is going to be my key and the url is the value associated with it.
I first try to declare an NSDictonary
as a private (global variable), but since I could not get it to work, I resorted to declare it in my header file as follows:
@property (nonatomic, retain) NSDictionary *bookmarks;
I synthesize it and I initialized it in the viewDidLoad as follows:
- (void)viewDidLoad
{
bookmarks = [NSDictionary
dictionaryWithObjectsAndKeys:@"http://www.microsoft.com",
@"Microsoft",
@"http://www.google.com",
@"Google",
@"http://www.apple.com",
@"Apple",
@"http://msdn.microsoft.com",
@"MSDN", nil];
[super viewDidLoad];
}
I then associated a control with my segmented control and when the event is triggered and the function is called I've got the following code which is called:
- (IBAction) getShortcut:(id)sender
{
NSString *shortcut;
shortcut = [shortcuts titleForSegmentAtIndex:shortcuts.selectedSegmentIndex];
NSString *url = [bookmarks valueForKey:shortcut];
//[self navigateTo: url];
[url release];
}
When a button from the UISegmentedControl
is clicked, I extract the value and stored it into shortcut and then I try to use the shortcut variable as a key to extract the associated value from the NSDictionary
"bookmarks" but it keeps crashing on NSString *url = [bookmarks valueForKey:shortcut];
and bombs out of my function and displays the usual error EXC_BAD_ACCESS
Any help would be greatly appreciated.
T.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有两个选择。一种是直接处理ivar,如 @Matt S. 发布的那样。请注意,在这种情况下,您需要让对象保持足够的保留计数。您正在使用自动释放的对象并导致错误。
另一种选择是使用您已经定义的属性:
并且该属性保留它。
You have two options. one is to deal with the ivar directly as @Matt S. posted. Note that in this case you need to keep you object with enough retain count. You're using and auto released object and causing the error.
The other option is to use the property you already defined:
And the property retains it.
该字典是自动发布的。
试试这个:
That dictionary is autoreleased.
Try this:
您没有保留
NSDictionary
。做:
You didn't retain the
NSDictionary
.Do:
问题是,您没有在 viewDidLoad 方法中保留“书签”。 Apple 文档中提到了一个命名约定:如果初始化方法以“init...”开头,则返回的对象将被保留,如果不是,您必须自己执行此操作。 “dictionaryWithObjectsAndKeys”返回保留计数为 0 的对象,这意味着在分配范围(您的 viewDidLoad 方法)之后,它会立即再次释放。
因此,只需
在初始化后加上 即可完成。另一个为您保留的解决方案
并且您不应该在操作中释放网址。一旦你发布了字典,它就会被发布
The problem is, that you do not retain "bookmarks" in the viewDidLoad method. There is an naming convention mentioned somewhere in the Apple docs: If an intialisation method starts with "init..." the returned object is retained, if not you have to do it yourself. The "dictionaryWithObjectsAndKeys" returns and object with retain count 0, which means, that after the scope of assignment (your viewDidLoad method) it is immediatly released again.
So just put a
after your initalisation and you are done. Another solution which does the retaining for you
And you shouldn't release the url in your action. It gets released, once you release the dictionary