Professional Web Applications Themes

Cocoa ; NSString -substringToIndex strange error - Mac Programming

Hello NG In my code i want to generate substrings in the way : NSRange myRange; NSString * SomeString = "example contens with line termination \r\n"; NSString * partOfSomeString; myRange = [SomeString rangeOfString:"\r\n"]; partOfSomeString = [SomeString substringToIndex:myRange.location]; Now the funny is, if I step through the code with the debuger everything works fine. At runtime I get a error - [NSCFString substringWithRange:] Range or index out of bounds. Thank you for your time and help Florian...

  1. #1

    Default Cocoa ; NSString -substringToIndex strange error

    Hello NG

    In my code i want to generate substrings in the way :

    NSRange myRange;
    NSString * SomeString = "example contens with line termination \r\n";
    NSString * partOfSomeString;

    myRange = [SomeString rangeOfString:"\r\n"];
    partOfSomeString = [SomeString substringToIndex:myRange.location];

    Now the funny is, if I step through the code with the debuger everything
    works fine.
    At runtime I get a error - [NSCFString substringWithRange:] Range or index
    out of bounds.

    Thank you for your time and help

    Florian


    Florian Guest

  2. #2

    Default Re: Cocoa ; NSString -substringToIndex strange error

    Florian Zschocke <de> wrote:
     

    This is probably diagnostic of some other problem. You're probably
    mismanaging memory somewhere else in your program. This can cause your
    string to be "replaced" by a different string. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  3. #3

    Default Re: Cocoa ; NSString -substringToIndex strange error

    matt neuburg wrote:
     

    Thank you Matt - that can be. But I also get this error when I check first.
     
    myRange = [SomeString rangeOfString:"\r\n"];
    if (myRange.length > 0){
    partOfSomeString = [SomeString substringToIndex:myRange.location];
    }

    Thank you.


    Florian Guest

  4. #4

    Default Re: Cocoa ; NSString -substringToIndex strange error

    In article <3fefef74$0$19064$arcor-online.net>,
    "Florian Zschocke" <de> wrote:
     
    >
    > Thank you Matt - that can be. But I also get this error when I check first.[/ref]

    Have you actually checked to see what range it's returning? Can you tell
    us what it is?

    It's *always* better to run things in the debugger, find out what values
    are coming out, and compare those values with what you expect to find.
    Posting an error message and some code is better than just posting an
    error and saying 'this doesn't work', but it's still not very good. Try
    to provide as much info as you can.
    Michael Guest

  5. #5

    Default Re: Cocoa ; NSString -substringToIndex strange error

    In article <3feebe99$0$19065$arcor-online.net>,
    "Florian Zschocke" <de> wrote:
     

    Followed by a clearly bogus example, wasting everyone's time. If you
    have some code that is confusing you, post the actual code instead of
    asking everyone to make guesses. At this point, the best advice anyone
    can probably give you is to tell you to look into using
    -componentsSeparatedByString: instead.
    Doc Guest

  6. #6

    Default Re: Cocoa ; NSString -substringToIndex strange error

    Florian Zschocke wrote: 
    >
    > Thank you Matt - that can be. But I also get this error when I check first.

    > myRange = [SomeString rangeOfString:"\r\n"];
    > if (myRange.length > 0){
    > partOfSomeString = [SomeString substringToIndex:myRange.location];
    > }
    >
    > Thank you.[/ref]

    You should be checking for (myRange.location != NSNotFound), rather than
    the length of the range..

    -jcr
    John Guest

  7. #7

    Default Re: Cocoa ; NSString -substringToIndex strange error

    John C. Randolph <idiom.com> wrote:
     
    > >
    > > Thank you Matt - that can be. But I also get this error when I check first.
    > > 
    > > myRange = [SomeString rangeOfString:"\r\n"];
    > > if (myRange.length > 0){
    > > partOfSomeString = [SomeString substringToIndex:myRange.location];
    > > }
    > >
    > > Thank you.[/ref]
    >
    > You should be checking for (myRange.location != NSNotFound), rather than
    > the length of the range..[/ref]

    Not so. The docs say: "If subString isn't found, returns a range of
    {NSNotFound, 0}." Therefore checking for a length of 0 ought to work
    perfectly well. That's how I do it in my own code, and it's never given
    me a problem. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  8. #8

    Default Re: Cocoa ; NSString -substringToIndex strange error

    Hello NG
    Thank you all, I found a solution, checking against SomeString-lenght works
    fine.Perhaps rangeOfString does not work well with a empty string.

    Florian


    Florian Guest

  9. #9

    Default Re: Cocoa ; NSString -substringToIndex strange error

     

    Thank you for your advice, but to see it right, you are wasting your time by
    your self and you can talk only about your time you have wasted.

    Florian


    Florian Guest

  10. #10

    Default Re: Cocoa ; NSString -substringToIndex strange error

    Florian Zschocke <de> wrote:
     

    No, that's not true. This line works just fine:

    NSLog("%i", ["" rangeOfString: "howdy"].length);

    It logs zero, as you would expect. So you have not solved the problem,
    since your explanation is incorrect. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  11. #11

    Default Re: Cocoa ; NSString -substringToIndex strange error

    In <1g6q8pe.1u858lozi313gN%com> matt neuburg wrote: 
    Hm, OK, so let's take a look at the code:

    NSString * grInfo=[[NSString alloc] initWithString:[conn get_line]];
    while(![grInfo isEqualToString:"done"])
    {
    if ([grInfo length] > 2) // done that course of the error - it is not
    nice but working.
    {
    posRange =[grInfo rangeOfString:"\r\n"];

    // if(posRange.length > 0){ uncomment this will produce the error

    grInfo = [grInfo substringToIndex:posRange.location];

    //}
    posRange = [grInfo rangeOfString:"\t\t"];
    if (posRange.length==0){
    posRange= [grInfo rangeOfString:"\t"];
    } }
    grTitel = [grInfo substringToIndex:posRange.location];
    if([grTitel length] > 0) {
    [newsGroup retain];
    [newsGroup setGroupName:grTitel];
    if([grInfo length] > ([grTitel length] + posRange.length)) {
    [newsGroup setGroupComment:[grInfo substringFromIndex:posRange.
    location + posRange.length]];
    } }
    [medial addObject:newsGroup];
    [newsGroup release]; // add this to array
    items++;

    } }
    } }
    grInfo=[conn get_line];
    } }

    // and the get_line providing the string

    - (NSString* ) get_line
    {
    struct timeval w_time;
    w_time.tv_sec=5;
    w_time.tv_usec=0;
    NSString * tmp = " ";
    fd_set rset;
    char* buffer=(char*)malloc(10);
    char* all=(char*) malloc (1024);
    FD_ZERO(&rset);
    FD_SET(fd,&rset);
    int val=0;

    val=select ((fd+1),&rset , NULL,NULL,&w_time);
    int counter=0;
    while(buffer[0]!='\n')
    {
    if (val>0)
    {

    if (FD_ISSET(fd,&rset))
    {
    recv(fd,buffer,1, 0);
    // printf("%s", buffer);
    if((counter==0) && buffer[0]=='.')
    {
    free(all);
    tmp="done";
    // buffer[0] = '\n';
    break;
    } }
    all[counter]=buffer[0];
    counter++;
    } }
    } }
    } }
    if(counter > 0)
    {
    all[counter]='\0';
    free(buffer);
    tmp=[[NSString alloc] initWithCString:all];
    free(all);
    } }
    return tmp;
    }

    Sorry, but I did not know how to act on the newsgroup. So i did not know
    if i should
    provide code here and how mutch.
    Thank you again for your time and help.
    Florian
    Florian Guest

  12. #12

    Default Re: Cocoa ; NSString -substringToIndex strange error

    Florian Zschocke <de> wrote:
     

    Sure, because if the substring was not found, posRange.location will be
    -1, which is not a valid index for substringToIndex. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  13. #13

    Default Re: Cocoa ; NSString -substringToIndex strange error

    In article <20031230002700992+arcor.de>,
    Florian Zschocke <de> wrote:
     

    So what happens if you use the proper method of detecting if something
    isn't found, namely if(posRange.location != NSNotFound)?

    Also, if you simply want to trim some characters off the end of your
    string, you should probably use stringByTrimmingCharactersInSet: or
    whatever the method is called. You can give it the newline character set
    and everything should be taken care of.
    Michael Guest

  14. #14

    Default Re: Cocoa ; NSString -substringToIndex strange error

    Florian Zschocke <de> wrote:
     
    > Hm, OK, so let's take a look at the code:
    >
    > NSString * grInfo=[[NSString alloc] initWithString:[conn get_line]];
    > while(![grInfo isEqualToString:"done"])
    > {
    > if ([grInfo length] > 2) // done that course of the error - it is not
    > nice but working.
    > {
    > posRange =[grInfo rangeOfString:"\r\n"];
    >
    > // if(posRange.length > 0){ uncomment this will produce the error
    >
    > grInfo = [grInfo substringToIndex:posRange.location];
    >
    > //}
    > posRange = [grInfo rangeOfString:"\t\t"];
    > if (posRange.length==0){
    > posRange= [grInfo rangeOfString:"\t"];
    > } }
    > grTitel = [grInfo substringToIndex:posRange.location];[/ref]

    I'm sure you have solved it by now, but in case you haven't: If grInfo
    initially contained "\r\n" ([grInfo length] == 2), the newline will be
    stripped by now, and grInfo will contain "". Hence, it will NOT contain
    neither "\t\t" nor "\t", and is bound to crash here.

    Per
    Per Guest

  15. #15

    Default Re: Cocoa ; NSString -substringToIndex strange error

    Per Bull Holmen <com> wrote:
     
    >
    > I'm sure you have solved it by now, but in case you haven't: If grInfo
    > initially contained "\r\n" ([grInfo length] == 2), the newline will be
    > stripped by now, and grInfo will contain "". Hence, it will NOT contain
    > neither "\t\t" nor "\t", and is bound to crash here.[/ref]

    The real problem is that the OP did not originally provide this code; he
    provided some completely different, made-up code that had nothing to do
    with the problem. This code makes the bug completely obvious; his
    made-up code, of course, did not. Doc O'Leary rightly complained about
    this at the time, but the OP just snapped at him. Some people can't be
    helped no matter how hard you try. m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

Similar Threads

  1. Replies: 5
    Last Post: December 19th, 09:18 AM
  2. NSString in NSDateFormat'ed cell
    By matt in forum Mac Programming
    Replies: 0
    Last Post: September 10th, 04:50 AM
  3. Cocoa: put returns in NSString to keep it to a certain width
    By Simon Slavin in forum Mac Programming
    Replies: 2
    Last Post: September 8th, 10:33 PM
  4. NSString -> c string warning
    By matt neuburg in forum Mac Programming
    Replies: 3
    Last Post: August 17th, 04:55 PM
  5. Escape sequence for unicode characters in NSString
    By Eric Raas in forum Mac Programming
    Replies: 2
    Last Post: July 9th, 02:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139