Jac*_*cob 7 objective-c nsstring ios
好的,所以这有点令人困惑(对我而言).我有一个字符串,它有一个我想要的数字.我用'/'包围了这个数字,这样我以后就可以得到这个数字了.
以下是我从中获取数字的方式:
if ([MYSTRING hasSuffix:@"mp"]) {
int start = 0;
int end = 0;
char looking = '/';
for(int i=0; i < MYSTRING.length; i++){
if (looking == [MYSTRING characterAtIndex:i]) {
if (start == 0) {
start = i;
}
else{
end = i + 1;
}
}
}
NSLog(@"%@", MYSTRING); //When i NSLOG here i get '2012-06-21 03:58:00 +0000/1/mp', 1 is the number i want out of the string, but this number could also change to 55 or whatever the user has
NSLog(@"start: %i, end: %i", start, end); //When i NSLOG here i get 'start: 25, end: 28'
NSString *number = [MYSTRING substringWithRange:NSMakeRange(start, end)];
number = [number stringByReplacingOccurrencesOfString:@"/" withString:@""];
if ([number intValue] > numberInt) {
numberInt = [number intValue];
}
Run Code Online (Sandbox Code Playgroud)
它不断崩溃,控制台说:
*终止应用程序由于未捕获的异常'NSRangeException',原因是: ' - [__ NSCFString substringWithRange:]:范围或索引越界'*第一掷调用堆栈:(0x1875d72 0x106ce51 0x1875b4b 0x184ea64 0x3a6c 0x1080713 0x1bf59 0x1bef1 0xd532e 0xd588c 0xd49f5 0x49a2f 0x49c42 0x290fe 0x1b3fd 0x17d2f39 0x17d2c10 0x17ebda5 0x17ebb12 0x181cb46 0x181bed4 0x181bdab 0x17d1923 0x17d17a8 0x18e71 0x200d 0x1f35)libc ++ abi.dylib:终止调用抛出异常
从我的计数范围是在范围内,我不知道为什么我得到这个错误?
任何帮助,将不胜感激.
谢谢
我认为你在NSMakeRange的语法上有困惑.就是这样的
NSMakeRange(<#NSUInteger loc#>, <#NSUInteger len#>)
Run Code Online (Sandbox Code Playgroud)
<#NSUInteger loc#>: 它是您要开始挑选或子串的位置.
<#NSUInteger len#>: 这是输出或子字符串的长度.
例:
Mytest12test
现在我想选择'12'
所以:
NSString *t=@"Mytest12test";
NSString *x=[t substringWithRange:NSMakeRange(6, 2)] ;
Run Code Online (Sandbox Code Playgroud)
在您的代码而不是长度中,您传递的是结束字符的索引,这是您的错误.
| 归档时间: |
|
| 查看次数: |
15887 次 |
| 最近记录: |