0
我現在使用它搜索HTML後返回的NSString功能, 的代碼是Objective-C的功能延遲
- (IBAction) analysys:(id)sender {
comparisonOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
NSString *coupangURL = @"http://www.coupang.com/alldeal.pang";
NSMutableArray * title = [[NSMutableArray alloc] init];
[title addObject:(NSString*)@"box"];
for (NSString * e in title) {
NSString * addr = [self searchCoupang:coupangURL targetString:e];
self.myTextView.text = addr;
}
}
- (NSString*) searchCoupang:(NSString *) url targetString:(NSString*) tString{
NSString *testString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]
encoding:NSUTF8StringEncoding
error:nil];
NSString * targetURL;
if (testString != NULL){
// Find the string
NSRange rangeOfTargetString = [testString rangeOfString:tString options:comparisonOptions];
t = [NSDate timeIntervalSinceReferenceDate];
if(rangeOfTargetString.location != NSNotFound) {
// Adjust range to take the line takes URL
NSRange cutRange = {rangeOfTargetString.location - cutStringValueToGetTheAddress, cutStringValueToGetTheAddress};
// Line which takes URL
NSString * lineContainsURL = [testString substringWithRange:cutRange];
NSRange rangeStartOfURL = [lineContainsURL rangeOfString:@"href"];
NSRange rangeEndOfURL = [lineContainsURL rangeOfString:@"onclick"];
NSRange targetRangeOfURL = {rangeStartOfURL.location + 6 ,((rangeEndOfURL.location - 2) - (rangeStartOfURL.location + 6)) };
targetURL = [lineContainsURL substringWithRange:targetRangeOfURL];
}
return targetURL;
}
else
return @"Reading url error";
}
工作正常,但是當我檢查的運行時間每個「searchCoupang功能「諸如此類
NSString *testString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]
encoding:NSUTF8StringEncoding
error:nil];
和
if (testString != NULL){
// Find the string
NSRange rangeOfTargetString = [testString rangeOfString:tString options:comparisonOptions];
t = [NSDate timeIntervalSinceReferenceDate];
if(rangeOfTargetString.location != NSNotFound) {
// Adjust range to take the line takes URL
NSRange cutRange = {rangeOfTargetString.location - cutStringValueToGetTheAddress, cutStringValueToGetTheAddress};
// Line which takes URL
NSString * lineContainsURL = [testString substringWithRange:cutRange];
NSRange rangeStartOfURL = [lineContainsURL rangeOfString:@"href"];
NSRange rangeEndOfURL = [lineContainsURL rangeOfString:@"onclick"];
NSRange targetRangeOfURL = {rangeStartOfURL.location + 6 ,((rangeEndOfURL.location - 2) - (rangeStartOfURL.location + 6)) };
targetURL = [lineContainsURL substringWithRange:targetRangeOfURL];
}
工作幾毫秒,但是當我查看了「搜索延時」時間延遲幾秒鐘後的功能。
for (NSString * e in title) {
// start to check the time
NSString * addr = [self searchCoupang:coupangURL targetString:e];
// End checking time (takes few seconds)
self.myTextView.text = addr;
}
此延遲來自哪裏?
感謝,
+1。我不認爲OP知道'-stringWithContentsOfURL:'代表他執行同步的'NSURLConnection'。 – 2011-12-31 01:17:59
實際加載的html文件的大小很大,但是當我用小尺寸的html文件(帶有URL)檢查它時,它的時間幾乎是相同的,所以我認爲它不是downlonding數據大小的問題。但連接到URL將是問題。而當我重複3〜4次時,它會變快。會不會因爲它保存了文件? – 2011-12-31 05:14:23
是的,它可能確實在緩存文件的過程中的某個時刻。 – 2011-12-31 05:21:10