将 Objective-C 对象与 NSDictionary 一起使用
我想针对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两行是问题的原因:
首先为属性分配一个可变数组。然后为该属性分配一个不同的不可变数组。您原来的可变数组会泄漏,因为您没有释放它。
异常是由这一行引起的:
当您到达它时,self.urlToLinkDictionary 是一个不可变的字典。你无法改变它。
还有其他问题:
These two lines are the cause of the issue:
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:
By the time you get to it, self.urlToLinkDictionary is an immutable dictionary. You can't change it.
There are other problems:
为什么不尝试更简化的方法。将标签与每个标签相关联。该标签是一个整数值,您可以使用它来定位数组中的 url。
因此,创建数组的代码。
然后您可以访问适当标签的 urlstring
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.
then you can access the urlstring for the appropriate label