NSString subStringToIndex gives EXC_BAD_ACCESS

  • The following code snippet compiles fine but gives an bad access error
    on run....any ideas

          NSString *projectName;
          NSString *title = @"This is a test";

          // and parse the project name to remove the [TM] from the end
          NSUInteger realLength = title.length - 4;
          if ( realLength > 0 )
          {
              [projectName initWithString:[title
    substringToIndex:realLength]];
          }

    Thanks

    Gregor
  • You didn't alloc projectName.

    On Nov 9, 2007, at 2:45 PM, Gregor Brandt wrote:

    > The following code snippet compiles fine but gives an bad access
    > error on run....any ideas
    >
    >
    > NSString *projectName;
    > NSString *title = @"This is a test";
    >
    > // and parse the project name to remove the [TM] from the end
    > NSUInteger realLength = title.length - 4;
    > if ( realLength > 0 )
    > {
    > [projectName initWithString:[title
    > substringToIndex:realLength]];
    > }
    >
    >
    > Thanks
    >
    > Gregor
  • > NSString *projectName;
    ...
    > [projectName initWithString:[title
    > substringToIndex:realLength]];

      Unless you have a very good reason not to, alloc and init... at the
    same time. This advice is repeated constantly and should not be
    ignored. :-)

    --
    I.S.
  • ack!  Beginner error....thanks

    Gregor

    On 9-Nov-07, at 3:52 PM  , I. Savant wrote:

    >> NSString *projectName;
    > ...
    >> [projectName initWithString:[title
    >> substringToIndex:realLength]];
    >
    >
    > Unless you have a very good reason not to, alloc and init... at the
    > same time. This advice is repeated constantly and should not be
    > ignored. :-)
    >
    > --
    > I.S.
    >
    >
previous month november 2007 next month
MTWTFSS
      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    
Go to today