访问 NSDictionary 问题

发布于 2024-11-24 07:19:10 字数 1659 浏览 1 评论 0原文

我只是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红焚 2024-12-01 07:19:10

你有两个选择。一种是直接处理ivar,如 @Matt S. 发布的那样。请注意,在这种情况下,您需要让对象保持足够的保留计数。您正在使用自动释放的对象并导致错误。

另一种选择是使用您已经定义的属性:

self.bookmarks = [[NSDictionary ...]];

并且该属性保留它。

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:

self.bookmarks = [[NSDictionary ...]];

And the property retains it.

書生途 2024-12-01 07:19:10

该字典是自动发布的。

试试这个:

self.bookmarks = [NSDictionary dictionaryWithObjectsAndKeys...]

That dictionary is autoreleased.

Try this:

self.bookmarks = [NSDictionary dictionaryWithObjectsAndKeys...]
岁月蹉跎了容颜 2024-12-01 07:19:10

您没有保留 NSDictionary

做:

bookmarks = [[NSDictionary
                 dictionaryWithObjectsAndKeys:@"http://www.microsoft.com",
                                              @"Microsoft", nil] retain];

You didn't retain the NSDictionary.

Do:

bookmarks = [[NSDictionary
                 dictionaryWithObjectsAndKeys:@"http://www.microsoft.com",
                                              @"Microsoft", nil] retain];
一世旳自豪 2024-12-01 07:19:10

问题是,您没有在 viewDidLoad 方法中保留“书签”。 Apple 文档中提到了一个命名约定:如果初始化方法以“init...”开头,则返回的对象将被保留,如果不是,您必须自己执行此操作。 “dictionaryWithObjectsAndKeys”返回保留计数为 0 的对象,这意味着在分配范围(您的 viewDidLoad 方法)之后,它会立即再次释放。

因此,只需

[bookmarks retain];

在初始化后加上 即可完成。另一个为您保留的解决方案

bookmarks = [[NSDictionary alloc] initWithObjectsAndKeys ...];

并且您不应该在操作中释放网址。一旦你发布了字典,它就会被发布

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

[bookmarks retain];

after your initalisation and you are done. Another solution which does the retaining for you

bookmarks = [[NSDictionary alloc] initWithObjectsAndKeys ...];

And you shouldn't release the url in your action. It gets released, once you release the dictionary

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文