2011-10-27 68 views
2

如何提取NSUInteger indexObjective-C的精華指數:

NSRange range = [string rangeOfString: substring] 

,所以我可以在[array objectAtIndex:index]寫這個指標?如果子在該字符串的開頭方式發現

回答

3
// make sure that the substring was actually found: 
NSRange result = [string rangeOfString:substring]; 

if (result.location != NSNotFound) 
{ 
    // make sure that the index exists in the array 
    if (result.location < [array count]) 
    { 
     id someObj = [array objectAtIndex:result.location]; 
     // do something cool with someObj here 
    } 
} 
3
NSRange range = [string rangeOfString:substring]; 
NSUInteger index = range.location; 
//... 
+0

range.location返回一個非常大的數字。爲什麼是這樣? – coolcool1994

+0

@ coolcool1994你找到原因了嗎?我有同樣的問題 – hoangpx