我需要获取重定向URL但在Swift中阻止重定向.从其他帖子和Apple文档我明白我必须实现委托方法URLSession(session:, task:, willPerformHTTPRedirection response:, request:, completionHandler:)并nil通过完成闭包返回.但是我无法在swift中找到示例,也找不到正确的方法.下面的代码在playground中重现了我的问题:委托似乎没有被执行.
import Foundation
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
class MySession: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {
// trying to follow instructions at https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionTaskDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionTaskDelegate/URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
// to prevent redirection -- DOES NOT SEEM TO GET CALLED
func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {
println("in URLSession delegate") // NEVER PRINTS
completionHandler(nil) // NO EFFECT
}
// fetch data from URL with NSURLSession
class func getDataFromServerWithSuccess(myURL: String, …Run Code Online (Sandbox Code Playgroud) 更新到XCode 7并将我的项目转换为最新的Swift 2语法后,有一个我似乎无法解决的错误.我有一个导航控制器的segue,需要将数据传递到其堆栈中的顶视图控制器.以下一直有效:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController.viewControllers[0] as! MYViewController
// OR let destinationVC = segue.destinationViewController.topViewController as! MYViewController
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是现在编译器给出了错误:
Value of type 'UIViewController' has no member 'viewControllers'
要么
Value of type 'UIViewController' has no member 'topViewController'
我没有看到如何访问堆栈上的视图控制器.有任何想法吗?提前致谢!