MGD
MGD

Reputation: 783

NSString Problem

NSRange rangetxt={0,(index-1)};
NSString *tempStr= textBox.text;
NSString *tempTextBox=[tempStr substringWithRange:rangetxt];

Hi Everyone,

I want to know why my pasted code isn't working properly, when the compiler passes substringWithRange It hung up and do not assign any value to tempTextBox, Secondly I've tried the NSString function substringToIndex as well yet its not working too, My objective is to skip the last character while copying tempStr to tempTextBox.

Regards MGD

Upvotes: 0

Views: 259

Answers (4)

Simon Whitaker
Simon Whitaker

Reputation: 20586

You don't show the code that's initialising index. My guess is it's out of range. Note that substringWithRange raises an NSRangeException if any part of the range lies beyond the end of the receiver.

Try this:

NSString *tempStr = textBox.text;
NSRange rangetxt;
if ([tempStr length] > 0)
    rangetxt = NSMakeRange(0, [tempStr length] - 1);
else
    rangetxt = NSMakeRange(0, 0); 
NSString *tempTextBox = [tempStr substringWithRange:rangetxt];

Upvotes: 1

MGD
MGD

Reputation: 783

I think i got my problem solved, During the debug when i pass through these line my gdb says "Timed out fetching data. Variable display may be inaccurate" so i found the solution just placed my break point after the initialization of the NSString objects

Thanks for Help Everyone

MGD

Upvotes: 0

BastiBen
BastiBen

Reputation: 19880

You should use NSMakeRange(loc, len) to create a NSRange object instead of using {}.

Upvotes: 2

Krešimir Prcela
Krešimir Prcela

Reputation: 4281

I had test this code and it works:

NSString *strTest = @"Word";
NSRange range = {0, 3};
NSString *strTemp = [strTest substringWithRange: range];

The result: strTemp = "Wor"

So the problem is something else: index is not proper or textBox.text is maybe empty.

Put the breakpoint on your substringWithRange: line and look at the values of index and tempStr before problem appears.

Upvotes: 2

Related Questions