2016-12-05 27 views
1

我正在爲我的應用程序編寫我的XCUITest。 我爲了使用waitForExpectationsWithTimeout,使我的測試異步我宣佈警報....但是它是在拋繩錯誤Variable used within its own initial value上的alert聲明5.Swift錯誤「在其初始值內使用的變量」

let timeout = NSTimeInterval() 
    let app = XCUIApplication() 

    let exists = NSPredicate(format: "exists == 1") 
    let alert = alert.buttons["OK"] 


    testCase.addUIInterruptionMonitorWithDescription("Enable Notifications") { (alert) -> Bool in 
      alert.buttons["OK"].tap() 
     return true 
    } 

    self.buttons["Enable notifications"].tap() 
    testCase.expectationForPredicate(exists, evaluatedWithObject: alert, handler: nil) 
    testCase.waitForExpectationsWithTimeout(timeout, handler: nil) 
    app.tap() 

誰能告訴我,爲什麼它是拋出這個錯誤,我可以做些什麼來解決這個問題。提前致謝。

回答

4

這是因爲在你的線沒有。 5,你已經寫了

let alert = alert.buttons["OK"] 

警報從來沒有在這行前宣佈,所以你不能寫這個。

例如,以這種情況下,

let a = a+5 

現在,編譯器會拋出同樣的錯誤,因爲它不知道的值「a」,因爲它沒有前宣佈。

+0

好吧,我以前怎麼聲明警報? –

+0

我對Xcode測試並不熟悉,只是回答了您的問題。搜索一下後在互聯網上發現這一點 - 讓alert = app.alerts.elementBoundByIndex(0).buttons [「OK」] - 嘗試一次 – Aakash

+0

如果沒有幫助,請嘗試此鏈接http://stackoverflow.com/questions/32148965/ xcode-7-ui-testing-how-to-dismiss-a-series-of-system-alerts-in-code或等待有此知識的人來回答 – Aakash