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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:01:13+00:00 2026-05-26T16:01:13+00:00

I am creating an imap client. I want to parse body and header of

  • 0

I am creating an imap client. I want to parse body and header of incoming data but it crashes. I couldn’t understand why it crashes and gives substring out of range error. How can I fix it?

I only want to check if the incoming string contains “FETCH” so I parse data, since string comes like
* FETCH or * 1 FETCH I thought checking isEqualToString range of (4,6) would be enough but that didn’t work.

- (NSString*) readLine
{
    NSMutableData* data = [[NSMutableData alloc] init];
    unsigned char c;
    for (;;) {
        recv(socket_, &c, sizeof(c), 0);
        if (c == '\n') {
            NSString* s = [[NSString alloc] initWithData: data
                                                encoding: NSUTF8StringEncoding];
            NSString *str = [s substringWithRange:NSMakeRange(4, 6)];

            if( [str isEqualToString:@"FETCH "]){
            NSMutableArray *substrings = [NSMutableArray new];
                NSScanner *scanner = [NSScanner scannerWithString:s];
                [scanner scanUpToString:@"}" intoString:nil];
                while(![scanner isAtEnd]) {
                    NSString *substring = nil;
                    [scanner scanString:@"}" intoString:nil];
                    if([scanner scanUpToString:@"*" intoString:&substring]) {
                        // If the space immediately followed the }, this will be skipped
                        [substrings addObject:substring];
                    }
                    [scanner scanUpToString:@"}" intoString:nil]; // Scan all characters before next }
                }
                NSString *email;
                [emailList addObject:@"Select an Email"];
                for(int i=0; i<substrings.count;i++){
                    email = [substrings objectAtIndex:i];
                    [emailList addObject:email]; // add emails in emailList
                }
                [substrings release];
            }


            if (nil != s) {
                NSLog(@"%@",s);
            }



            [data release];
            return [s autorelease];
        } 
            else {
            [data appendBytes: &c length: 1];
        }
    }
    return nil;
}

output is:

* 1 FETCH (BODY[HEADER.FIELDS (FROM SUBJECT DATE)] {149}
2011-11-07 23:32:24.363 SwitchDeneme[327:bc03] Date: Mon, 07 Nov 2011 17:00:25 -0500 (EST)
2011-11-07 23:32:24.364 SwitchDeneme[327:bc03] From: "AOLWelcomeInfo" <AOLWelcomeInfo@message.aol.com>
2011-11-07 23:32:24.365 SwitchDeneme[327:bc03] Subject: Welcome to Your New Email Account!
2011-11-07 23:32:24.367 SwitchDeneme[327:bc03] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString substringWithRange:]: Range or index out of bounds'

terminate called throwing an exceptionsharedlibrary apply-load-rules all
  • 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-05-26T16:01:14+00:00Added an answer on May 26, 2026 at 4:01 pm

    The problem is that your string is likely shorter than 7 characters, meaning it does not have an index of 6.

    Try something more like this:

    NSRange range = [someString rangeOfString:@"FETCH "];
    if( range.location != NSNotFound ) {
       //found it... so now do you processing...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a web-based IMAP client in Rails, and want to cache the
What's the best general technique for creating an IMAP client and keeping its local
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I am creating Email client using JavaMail API. Everything is working fine like I
Creating extensions got much easier with Vs2010, but this seems not to be the
Creating the closure is easy but using it is confusing for me. Here is
I'm creating a generator that gets consumed by another function, but I'd still like
Creating My Windows Form Application and using ADO.Net as Data Access layer and SQL
Creating a website using simple html(not html5) and css but got stock in mouseover
Creating a google map with store locations within 50 miles of user entered address.

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.