Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 268045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:38:21+00:00 2026-05-11T23:38:21+00:00

I am new to Objective-C and I have no clue why this code is

  • 0

I am new to Objective-C and I have no clue why this code is not working:

    NSMutableDictionary *bookmarks = [NSMutableDictionary dictionaryWithCapacity:(NSUInteger) 4];
[bookmarks setObject:@"Stanford University" forKey:[NSURL URLWithString:(NSString *) @"http://www.stanford.edu"]];
[bookmarks setObject:@"Apple" forKey:[NSURL URLWithString:(NSString *) @"http://www.apple.com"]];
[bookmarks setObject:@"Berkeley" forKey:[NSURL URLWithString:(NSString *) @"http://www.berkeley.edu"]];
[bookmarks setObject:@"CS193P" forKey:[NSURL URLWithString:(NSString *) @"http://cs193p.stanford.edu"]];

NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
    url = [browser valueForKey:(NSString *)each];
    NSLog(@"%@", [url absoluteURL]);
}

The error I get is:

2009-06-29 11:25:22.844 WhatATool[2102:10b] *** -[NSURL length]: unrecognized selector sent to instance 0x1072c0
2009-06-29 11:25:22.845 WhatATool[2102:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL length]: unrecognized selector sent to instance 0x1072c0'

Any help would be appreciated.
Thanks.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:38:22+00:00Added an answer on May 11, 2026 at 11:38 pm

    A key is what you use to access an object in the dictionary. You have your keys and objects reversed on every line you call -[bookmarks setObject:... forKey...] in the code. The problem in your case arises because you’re trying to treat NSURL objects as NSString objects — [bookmarks objectForKey:(NSString *)each] — and the dictionary is trying to get the length of the supposed string by calling -length, which exists for NSString but not for NSURL.

    If you’re always constructing a dictionary with the same objects, consider using a more compact “varargs” constructor for NSDictionary. (Note that the last comma-separated argument must be nil — see the related documentation) You can also use -[NSDictionary dictionaryWithObjects:forKeys:] with two NSArray objects, containing the URLs and the bookmark names. (Incidentally, casting string literals as NSString* to create an NSURL is completely unnecessary.)

    NSDictionary *bookmarks = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSURL URLWithString:@"http://www.stanford.edu"],
                               @"Stanford University",
                               [NSURL URLWithString:@"http://www.apple.com"],
                               @"Apple",
                               [NSURL URLWithString:@"http://www.berkeley.edu"],
                               @"Berkeley",
                               [NSURL URLWithString:@"http://cs193p.stanford.edu"],
                               @"CS193P",
                               nil];
    

    In addition, you have a lurking error that hasn’t been mentioned yet: browser is an NSEnumerator, but you’re calling valueForKey: as if it were an NSDictionary, rather than on bookmarks. That will cause a similar crash for an unrecognized selector, too. (Even for dictionary objects, you should call -objectForKey: instead; -valueForKey: is used mostly for/by Cocoa Bindings, and does extra work you don’t need. I realize it’s confusing since we think in terms of “key-value pairs”, but there it is…)

    Finally, you can also simplify the enumeration code somewhat. (A for-in loop on an NSDictionary enumerates its keys just like -keyEnumerator would.)

    Here’s how I’d suggest doing the last part:

    NSURL *url
    for (NSString *key in bookmarks) {
        url = [bookmarks objectForKey:key];
        NSLog(@"%@", [url absoluteURL]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Objective-C and I have been working with simple programs, and I
I am new to objective c,so plz me help with this... i have used
Im new to objective-c Lets assume I have a string such as this NSString*
I am new to Objective-C and iPhone I have to sort a NSDictionary using
I'm new in objective-c. I have a path to file contained in an NSString
I'm new to objective-c and at the moment developing a small application. I have
I'm very new to Objective-C, and am having some beginner issues. I have an
Hello I am new to the Objective C and I have a problem. I
I'm new to programming and stackoverflow and have decided to start by learning objective
I'm new to developing with xCode and Objective C, and I have a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.