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 7931161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:38:24+00:00 2026-06-03T20:38:24+00:00

I have made a program that gets some strings in input from the user

  • 0

I have made a program that gets some strings in input from the user (it stops typing “exit”), puts them in a NSMutableArray and sort them:

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

NSInteger compare( id id1, id id2, void* context)
{
    NSString* str1=[id1 stringValue], *str2=[id2 stringValue]; /* ****** exception here */
    const char* buf1, *buf2;
    buf1=[str1 UTF8String];
    buf2=[str2 UTF8String];
    if(strcmp(buf1,buf2)<0)
        return NSOrderedAscending;
    else
        return NSOrderedDescending;
}


int main (int argc, const char * argv[])
{
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
    NSMutableArray* array=[[NSMutableArray alloc]init];
    NSString* str=@"";
    char buffer[100];
    NSLog(@"Type a sequence of strings, ending with 'exit', the strings will be sorted in alphabethical order");
    while(! [str isEqualToString:@"exit\n"])
    {
        fgets(buffer,100,stdin);
        str=[NSString stringWithUTF8String: buffer];
        [array addObject: str];

    }
    [array sortUsingFunction: compare context: NULL];
    [array release];
    [pool drain];
    return 0;
}

But I get an exception in the line that I have marked with “**“, it says:

2012-05-09 00:10:14.502 CLITest[1029:903] -[NSCFString stringValue]: unrecognized selector sent to instance 0x10010cbf0

It seems like it doesn’t take the string value from the id.Why? How should I change the comparator method?

  • 1 1 Answer
  • 0 Views
  • 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-06-03T20:38:25+00:00Added an answer on June 3, 2026 at 8:38 pm

    id1 and id2 are actually NSStrings, even if they are casted to id and passed to your function (this approach is useful because you can define your compare function in order to sort custom objects).

    It doesn’t make sense to call stringValue on a string (and, in fact, NSString do not have a member function named stringValue).

    To work with strings just cast back to NSStrings.

    NSString *s1 = (NSString*) id1;
    NSString *s2 = (NSString*) id2;
    

    This is not strictly necessary (you can even work directly with id1 and id2) but casting will avoid you to get compiler warnings (also it’s a clearer code).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a program that scans rss feeds. This same program creates feeds
I have made a program with vb.net and I want that program to run
I have made a script that uses a program called Diascope, its a video
I have a pointer of a structure type that I made. On program start
I have some SerialPort code that constantly needs to read data from a serial
I have a codefirst EF-4.1 based program. The user gets a context and can
I have made a demo small program that I want to deliver to my
I have an iOS app which gets some JSON from a server (in the
I have made a program in c and wanted to see, how much memory
I have made a registration program. Making use of mysql database. Can I still

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.