iOS 12學習系列:針對Xcode的警告忽略消除處理

來源:互聯網
上載者:User

標籤:

問題描述

html代碼如下

 1 <html> 2     <head> 3         <meta charset="utf-8"/> 4         <title>我的網頁</title> 5         <script type="text/javascript"> 6             function JS2OC() 7             { 8                 window.location.href="fzw://send"; 9             }10         </script>11     </head>12     <body>13         <input value="js調用oc" type="button" onclick=‘JS2OC();‘></input>14     </body>15 </html>

顯示效果如下

 oc代碼如下

 1 /** 2  *  跳轉判斷 3  */ 4 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 5 { 6     NSString *urlString = request.URL.absoluteString; 7     NSLog(@"urlString:%@",urlString); 8     NSString *preString = @"fzw://"; 9     if([urlString hasPrefix:preString])10     {11         NSString *methodString = [urlString substringFromIndex:preString.length];12         NSLog(@"methodString:%@",methodString);13         [self performSelector:NSSelectorFromString(methodString)];14         return NO;15     }16     return YES;17 }18 19 -(void)send20 {21     NSLog(@"%s",__func__);22 }

 

點擊demo.html的按鈕“js調用oc”,網頁跳轉到fzw://send。UIWebView的代理方法- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType攔截頁面並擷取網頁連結fzw://send,經過處理,最後調用oc指定方法-(void)send,並返回NO取消跳轉,從而達到js調用oc方法。

但xcode提示警告: PerformSelector may cause a leak because its selector is unknown

問題分析

編譯器的警告對開發人員來說是很有用的資訊,但有時由於編譯器的智商實在太低,會提示一些無謂的警告。當有些警告不想看到時,可以用如下代碼消除警告。

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-W警告名稱"
        需要關閉警告的代碼
#pragma clang diagnostic pop

問題解決

該警告的名稱為-Warc-performSelector-leaks

 1 /** 2  *  跳轉判斷 3  */ 4 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 5 { 6     NSString *urlString = request.URL.absoluteString; 7     NSLog(@"urlString:%@",urlString); 8     NSString *preString = @"fzw://"; 9     if([urlString hasPrefix:preString])10     {11         NSString *methodString = [urlString substringFromIndex:preString.length];12         NSLog(@"methodString:%@",methodString);13 #pragma clang diagnostic push14 #pragma clang diagnostic ignored "-Warc-performSelector-leaks"15         [self performSelector:NSSelectorFromString(methodString)];16 #pragma clang diagnostic pop17         return NO;18     }19     return YES;20 }21 22 -(void)send23 {24     NSLog(@"%s",__func__);25 }

 

iOS 12學習系列:針對Xcode的警告忽略消除處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.