2011-10-14 57 views
0

我有這樣的代碼IOS:錯誤的home目錄

NSString *nextSequentialFile = 
    [filePath stringByReplacingOccurrencesOfString:photoNumber 
    withString:[NSString stringWithFormat:@"%d", (index + 1)] 
    options:NSBackwardsSearch 
    range:[filePath rangeOfString:photoNumber options:NSBackwardsSearch]]; 

但我有這個錯誤,我不明白怎麼一回事,因爲它發生。

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString replaceOccurrencesOfString:withString:options:range:]: Range or index out of bounds' 

回答

1

嘗試:

NSString *nextSequentialFile = 
[filePath stringByReplacingOccurrencesOfString:photoNumber 
withString:[NSString stringWithFormat:@"%d", (index + 1)] 
options:NSBackwardsSearch 
range:NSMakeRange(0, filePath.length)]; 
+0

範圍:NSRangeFromString(filePath)]; //範圍或索引超出範圍' – CrazyDev

+0

嘗試使用'NSMakeRange(0,filePath.length)'而不是'NSRangeFromString'。查看修改。 – chown

0

很可能rangeOfString:photoNumber超出範圍。我建議記錄你傳遞給這個方法的每個值,以便告訴哪個是錯誤的。

0

不要假設像rangeOfString:options:回報的有效範圍的方法。從該文檔:

返回值

一種NSRange給出結構的位置和長度在 ASTRING的第一次出現的接收機中,在模掩模 的選項。 如果未找到aString或爲空(@「」),則返回{NSNotFound,0}。

[強調]

所以得到的範圍內的第一和嘗試使用它之前檢查NSNotFound