[英]Using objective-c objects with an NSDictionary
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. 我想针对UILabel存储URL,以便当用户触摸标签时将其带到UIWebView中的URL。
I have declared a NSDictionary like so: 我已经这样声明了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];
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? 考虑到我使用
NSValue
作为键,考虑到我仅对UILabels的引用,如何获取/设置与该键关联的URL?
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 但出现错误:“ objc_exception_throw”已解决
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. 在您熟悉它时,self.urlToLinkDictionary是一个不变的字典。 You can't change it.
您无法更改。
There are other problems: 还有其他问题:
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 那么您可以访问相应标签的urlstring
NSString* url = [arrayURLs objectAtIndex:labelClicked.tag];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.