【问题标题】:How to Check the string is same or changed in Objective C如何在Objective C中检查字符串是否相同或更改
【发布时间】:2013-01-30 09:41:45
【问题描述】:

我用下面的代码检查任何子类的框架,是否有变化。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if(imageFrameSize ==  CGRectZero) {
            imageFrameSize = self.recycleBin.frame;
            NSLog(@"Frame: %@", NSStringFromCGRect(imageFrameSize));
        }
}

现在我想检查字符串是否相同或已更改。基本上我将历史记录存储在history.plist 中。我在下面的委托中运行函数以将文档 url 保存在 history.plist 文件中。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //when push to reload is done it will disappear pulltorefresh
    [(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    [self actualizeButtons];


   urlPage =[webView stringByEvaluatingJavaScriptFromString:@"document.title"];    //[[[webView request] URL]absoluteString];

   [self addHistoryFunction];


    NSLog(@"WebView Finish Load %@",[[[webView request] URL]absoluteString] );

}

这个功能很好用。但问题是 webViewDidiFinishLoad 为 google 运行了几乎两次,为其他网站运行了 3 次或多次。所以 addHistoryFunction 使用相同的页面 url 保存历史两次或多次。 我想在添加到历史记录之前检查 urlPage 字符串,如果它是更新的新字符串,则运行 addhistory 函数,如果相同则跳过 addhistory 函数。就像上面的 CGRectZero 代码一样。

这就是我在 history.plist 中保存历史的想法。 如果有更好的然后我的请帮助我或解决我的问题。 提前致谢。

【问题讨论】:

    标签: objective-c xcode uiwebview nsstring


    【解决方案1】:

    - (void)webViewDidFinishLoad:(UIWebView *)webView 在 webview 完成加载网页中的对象时调用,因此最好的方法是防止多次调用此方法以添加此行:

    if ([webView isLoading]){return;)
    

    这一行将确保您的 webview 将在其仅完成加载时执行命令。 这是你编辑后的功能试试吧。

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
         if ([webView isLoading]){return;)
        //when push to reload is done it will disappear pulltorefresh
        [(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading];
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
        [self actualizeButtons];
    
    
       urlPage =[webView stringByEvaluatingJavaScriptFromString:@"document.title"];    //[[[webView request] URL]absoluteString];
    
       [self addHistoryFunction];
    
    
        NSLog(@"WebView Finish Load %@",[[[webView request] URL]absoluteString] );
    
    }
    

    【讨论】:

    • 不.. 它仍然运行 NSLog(@"WebView Finnish Load... ); 两次用于谷歌,更多用于其他网站....
    【解决方案2】:

    如果你想和字符串进行比较

    BOOL isTheSameStrings=[mystring isEqualToString:string2];
    if (isTheSameStrings){
       NSLog(@"the are the same);
    }else{
       NSLog(@"the are NOT the same);
    }
    

    或者你想检查是否在数组中找到了字符串

    BOOL myArrayDoesContainMyString=[myArray containsObject:myString];
    if (myArrayDoesContainMyString){
       NSLog(@"myArray contain myString);
    }else{
       NSLog(@"myArray does not contain myString);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多