【问题标题】:SwiftKeychainWrapper xctest returning nilSwiftKeychainWrapper xctest 返回 nil
【发布时间】:2016-05-15 18:36:22
【问题描述】:

我正在尝试使用钥匙串进行基本的单元测试。我可以成功设置键的值('setString' 返回 true),但是,当我尝试检索值时,即使延迟 5 秒后,返回仍然为零:

class MyKeychainTest: XCTestCase {
    func checkKeychain(timer: NSTimer) {
        debugPrint("check keychain...")
        let userInfo = timer.userInfo as! [String: AnyObject]
        let expectation = userInfo["expectation"] as! XCTestExpectation

        let res = KeychainWrapper.objectForKey("myKey")
        debugPrint("got res: \(res)")
        XCTAssertNotNil(res)

        expectation.fulfill()
    }

    func testKeychain() {
        let expectation = expectationWithDescription("gotKey")
        let success = KeychainWrapper.setString("foo", forKey: "myKey")
        debugPrint("set key?: \(success)")

        NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(self.checkKeychain)
        , userInfo: ["expectation": expectation ], repeats: false)

        self.waitForExpectationsWithTimeout(10, handler: nil)
    }
}

有什么可能导致这种情况的想法吗?

谢谢

【问题讨论】:

    标签: keychain xctest


    【解决方案1】:

    不小心使用了错误的方法来获取密钥。而不是 KeychainWrapper.objectForKey("myKey") 使用 KeychainWrapper.stringForKey("myKey") 工作。奇怪的是,在前一种情况下甚至没有返回一个对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 2019-06-20
      • 2017-12-16
      • 2014-09-27
      • 2017-06-11
      • 2014-12-29
      相关资源
      最近更新 更多