简体   繁体   English

NSString问题

[英]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 . 我想知道为什么我的粘贴的代码无法正常工作,当编译器传递substringWithRange挂起并且不给tempTextBox任何值,其次我也尝试了NSString函数substringToIndex ,但它也无法正常工作,我的目标是在将tempStr复制到tempTextBox时跳过最后一个字符。

Regards MGD 关于MGD

您应该使用NSMakeRange(loc, len)创建一个NSRange对象,而不要使用{}

I had test this code and it works: 我已经测试过此代码,并且可以正常工作:

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

The result: strTemp = "Wor" 结果:strTemp =“ Wor”

So the problem is something else: index is not proper or textBox.text is maybe empty. 所以问题就出在别的地方:索引不合适或textBox.text可能为空。

Put the breakpoint on your substringWithRange: line and look at the values of index and tempStr before problem appears. 将断点放在substringWithRange:行上,然后在问题出现之前查看index和tempStr的值。

You don't show the code that's initialising index . 您不会显示初始化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. 请注意,如果范围的任何部分超出接收器的末尾,则substringWithRange会引发NSRangeException

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];

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 我想我的问题解决了,在调试过程中,我通过这些行时,我的gdb说“超时,无法获取数据。变量显示可能不准确”,因此我发现该解决方案只是在初始化NSString对象之后放置了断点

Thanks for Help Everyone 感谢大家的帮助

MGD MGD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM