将 Objective-C 对象与 NSDictionary 一起使用

发布于 2024-08-31 22:36:10 字数 1282 浏览 5 评论 0原文

我想针对 UILabel 存储一个 URL,以便当用户触摸该标签时,会将他们带到 UIWebView 中的该 URL。

我已经声明了一个 NSDictionary,如下所示:

NSMutableArray *linksArray = [[NSMutableArray alloc] init];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem1ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem2ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem3ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem4ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem5ReadMoreLabel]];
//NSString *ageLink = @"http://www.theage.com.au";
NSArray *defaultLinks = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", nil];
self.urlToLinkDictionary = [[NSMutableDictionary alloc] init];
self.urlToLinkDictionary = [NSDictionary dictionaryWithObjects:defaultLinks forKeys:linksArray];

考虑到我使用 NSValue 作为键,鉴于我只引用了 UILabels,如何获取/设置与该键关联的 URL?

这就是我所拥有的,但它不起作用:

for(NSValue *key in [self.urlToLinkDictionary allKeys])
{
    if ([key nonretainedObjectValue] == linkedLabel)
    {
        [self.urlToLinkDictionary setValue:[newsItem link] forKey: key];
    }
}

但我收到错误:“objc_exception_throw”已解决

I want store a URL against a UILabel so that when a user touches the label it takes them to that URL in a UIWebView.

I have declared a NSDictionary like so:

NSMutableArray *linksArray = [[NSMutableArray alloc] init];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem1ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem2ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem3ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem4ReadMoreLabel]];
[linksArray addObject: [NSValue valueWithNonretainedObject: newsItem5ReadMoreLabel]];
//NSString *ageLink = @"http://www.theage.com.au";
NSArray *defaultLinks = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", nil];
self.urlToLinkDictionary = [[NSMutableDictionary alloc] init];
self.urlToLinkDictionary = [NSDictionary dictionaryWithObjects:defaultLinks forKeys:linksArray];

Considering I used a NSValue as the key, how do I get/set the URL associated with that key given that I only have references to the UILabels?

this is what I have but it doesn't work:

for(NSValue *key in [self.urlToLinkDictionary allKeys])
{
    if ([key nonretainedObjectValue] == linkedLabel)
    {
        [self.urlToLinkDictionary setValue:[newsItem link] forKey: key];
    }
}

but I get an error: "objc_exception_throw" resolved

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天气好吗我好吗 2024-09-07 22:36:10

这两行是问题的原因:

self.urlToLinkDictionary = [[NSMutableDictionary alloc] init];
self.urlToLinkDictionary = [NSDictionary dictionaryWithObjects:defaultLinks forKeys:linksArray];

首先为属性分配一个可变数组。然后为该属性分配一个不同的不可变数组。您原来的可变数组会泄漏,因为您没有释放它。

异常是由这一行引起的:

[self.urlToLinkDictionary setValue:[newsItem link] forKey: key];

当您到达它时,self.urlToLinkDictionary 是一个不可变的字典。你无法改变它。

还有其他问题:

  • linksArray 泄漏,因为你从未释放它。
  • newsItem1ReadMoreLabel 等。它们是什么类型?为什么你要把它们包裹在价值观中?
  • setValue:forKey: 是键值编码的一部分。在可变字典上它可以工作,但是通过键访问对象的正确方法是 setObject:forKey:通过
  • 字典的键进行线性搜索来搜索字典似乎有点毫无意义。

These two lines are the cause of the issue:

self.urlToLinkDictionary = [[NSMutableDictionary alloc] init];
self.urlToLinkDictionary = [NSDictionary dictionaryWithObjects:defaultLinks forKeys:linksArray];

First you assign a mutable array to the property. Then you assign a different immutable array to the property. Your original mutable array leaks because you don't release it.

The exception is being caused by this line:

[self.urlToLinkDictionary setValue:[newsItem link] forKey: key];

By the time you get to it, self.urlToLinkDictionary is an immutable dictionary. You can't change it.

There are other problems:

  • linksArray leaks because you never release it.
  • newsItem1ReadMoreLabel etc. What type are they? Why are you wrapping them in values?
  • setValue:forKey: is part of key value coding. On a mutable dictionary it works, but the correct method for accessing objects by keys is setObject:forKey:
  • it seems a bit pointless to search a dictionary by doing a linear search through its keys.
余生一个溪 2024-09-07 22:36:10

为什么不尝试更简化的方法。将标签与每个标签相关联。该标签是一个整数值,您可以使用它来定位数组中的 url。

因此,创建数组的代码。

NSMutableArray* arrayURLs = [NSMutableArray arrayWithCapacity:2];
newsItem1ReadMoreLabel.tag = 0;
[arrayURLs insertObject:urlStringforLabel1 atIndex:newsItem1ReadMoreLabel.tag];
newsItem2ReadMoreLabel.tag = 1;
[arrayURLs insertObject:urlStringforLabel2 atIndex:newsItem2ReadMoreLabel.tag];

然后您可以访问适当标签的 urlstring

NSString* url = [arrayURLs objectAtIndex:labelClicked.tag];

Why not try a more simplified approach. Associate a tag with every label. The tag is a in integer value and you can use it to locate the url in an array.

So code for creating the array.

NSMutableArray* arrayURLs = [NSMutableArray arrayWithCapacity:2];
newsItem1ReadMoreLabel.tag = 0;
[arrayURLs insertObject:urlStringforLabel1 atIndex:newsItem1ReadMoreLabel.tag];
newsItem2ReadMoreLabel.tag = 1;
[arrayURLs insertObject:urlStringforLabel2 atIndex:newsItem2ReadMoreLabel.tag];

then you can access the urlstring for the appropriate label

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