2013-03-08 24 views
-2

我是iphone開發noobot,我想解析一個字符串的子字符串。我解析的子字符串在每次解析時都會有所不同,所以我使用帶有範圍的子字符串來指示子字符串總是在兩個字符之間。問題是,當我得到一個異常,說異常 - [__ NSCFString substringWithRange:]:範圍或索引超出界限。任何幫助是極大的讚賞。如何恰當地爲子串分析字符串?

MY CODE

NSString * storyLink = @"http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2212677853001?src=mrss"//<--Parsing the numbers between "bctid" & "?src" 
NSRange start = [storyLink rangeOfString:@"d" options:NSBackwardsSearch]; 
NSRange end = [storyLink rangeOfString:@"?" options:NSBackwardsSearch]; 
NSString *clipid = [storyLink substringWithRange:NSMakeRange(start.location, end.location)];//<--Exception thrown here 
NSLog(@"clipid: %@", clipid); 

回答

2

範圍應解釋爲在形式{ begin, length }。有一個原因NSRange結構的成員被稱爲locationlength,而不是beginend。更改您的代碼爲

NSMakeRange(start.location, end.location - start.location) 

它會正常工作。

+0

謝謝!我現在知道了。我正在接近這個喜歡它是JAVA。 – 2013-03-08 23:41:21