Using objective-c objects with an NSDictionary

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-05-08T01:40:38Z Indexed on 2010/05/08 1:48 UTC
Read the original article Hit count: 371

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ipad