簡體   English   中英

Xcode 8 / Swift 3:“UIViewController類型的表達式?未使用的“警告

[英]Xcode 8 / Swift 3: “Expression of type UIViewController? is unused” warning

我有以下功能,以前干凈地編譯,但使用Xcode 8生成警告。

func exitViewController()
{
    navigationController?.popViewController(animated: true)
}

“表達式類型”UIViewController?“未使用”。

為什么要說這個,有沒有辦法刪除它?

代碼按預期執行。

TL; DR

popViewController(animated:)返回UIViewController? ,並且編譯器正在發出警告,因為您沒有捕獲該值。 解決方案是將其分配給下划線:

_ = navigationController?.popViewController(animated: true)

Swift 3改變

在Swift 3之前,默認情況下所有方法都有“可丟棄的結果”。 如果未捕獲方法返回的內容,則不會發出警告。

為了告訴編譯器應該捕獲結果,您必須在方法聲明之前添加@warn_unused_result 它將用於具有可變形式的方法(例如sortsortInPlace )。 您可以添加@warn_unused_result(mutable_variant="mutableMethodHere")來告訴編譯器。

但是,使用Swift 3時,行為會被翻轉。 現在所有方法都警告不會捕獲返回值。 如果要告訴編譯器不需要警告,請在方法聲明之前添加@discardableResult

如果您不想使用返回值,則必須通過將其分配給下划線來明確告訴編譯器:

_ = someMethodThatReturnsSomething()

將此添加到Swift 3的動機:

  • 防止可能的錯誤(例如使用sort思維它修改集合)
  • 不捕獲或需要捕獲其他協作者的結果的明確意圖

UIKit API似乎落后於此,沒有為完全正常(如果不是更常見)使用popViewController(animated:)添加@discardableResult而不捕獲返回值。

閱讀更多

當生活給你檸檬時,做一個擴展:

import UIKit

extension UINavigationController {
    func pop(animated: Bool) {
        _ = self.popViewController(animated: animated)
    }

    func popToRoot(animated: Bool) {
        _ = self.popToRootViewController(animated: animated)
    }
}

注意添加類似@discardableResult func pop(animated: Bool) -> UIViewController? 將導致您試圖避免的相同警告。

通過擴展,您現在可以編寫:

func exitViewController()
{
    navigationController?.pop(animated: true)
}

func popToTheRootOfNav() {
    navigationController?.popToRoot(animated: true)
}

編輯:也添加了popToRoot。

在Swift 3中,忽略具有聲明的返回值的函數的返回值會導致警告。

選擇退出此方法的一種方法是使用@discardableResult屬性標記該函數。 由於您無法控制此功能,因此無法使用。

擺脫警告的另一種方法是將值賦給_ 這告訴編譯器您知道該方法返回一個值,但您不希望將其保留在內存中。

let _ = navigationController?.popViewController(animated: true)

截圖1

雖然work correctly if kept as it isnumber of warning increases.number of warning increases.

解決方案是簡單地replace it with underscore ( _ )雖然它看起來很難看。

Eg.  _ = navigationController?.popViewController(animated: true)

截圖2

在這種情況下使用discardableResult

根據<Swift Programming Language>,語言參考 - 屬性一章。

discardableResult

將此屬性應用於函數或方法聲明,以在調用返回值的函數或方法而不使用其結果時抑制編譯器警告。

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/doc/uid/TP40014097-CH35-ID347

還有<Swift Programming Language>中的演示,語言指南 - 方法一章。

@discardableResult
    mutating func advance(to level: Int) -> Bool {
    ...
return true
}

因為調用advance(to :)方法忽略返回值的代碼不一定是錯誤,所以此函數標有@discardableResult屬性。 有關此屬性的更多信息,請參閱屬性。

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html#//apple_ref/doc/uid/TP40014097-CH15-ID234

如果你想像CodeReaper的答案那樣走上擴展之路,你應該使用@descardableResult 這保留了所有可能性,但使警告靜音。

import UIKit

extension UINavigationController {
    @discardableResult func pop(animated: Bool) -> UIViewController? {
        return self.popViewController(animated: animated)
    }

    @discardableResult func popToRoot(animated: Bool) -> [UIViewController]? {
        return self.popToRootViewController(animated: animated)
    }
}

另一種方法是你可以解開self.navigationController? value並調用popViewController函數。

    if let navigationController = navigationController {
        navigationController.popViewController(animated: true)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM