2015-08-19 17 views
10

mam oszczędności niektóre dane do zmiennej appdelegate z viewcontroller & ściągam go z innego widoku controller.Below jest kod aplikacji delegataGlobalna zmienna w Appdelegate w szybkim

class AppDelegate: UIResponder, UIApplicationDelegate { 
var window: UIWindow? 
var navigationController: UINavigationController? 
var mainDic:NSMutableDictionary? 

Kod ustawić mainDic

func filterResponse(response:NSDictionary){ 

    var appDelegate=AppDelegate() 
    appDelegate.mainDic=response.mutableCopy() as? NSMutableDictionary 
} 

Kod do pobrania słownika.

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
println(appDelegate.mainDic) 

Problem polega na tym, że otrzymuję wynik zerowy. Proszę, popraw mnie.

+1

To nie jest celem appDelegate. Przeczytaj kilka samouczków, takich jak [this one] (http://www.raywenderlich.com/86477/introducing-ios-design-patterns-in-swift-part-1) –

Odpowiedz

19

To twój błąd

var appDelegate=AppDelegate() //You create a new instance, not get the exist one 

Trzeba wejść do wychodzącego AppDelegate

let appDelegate = UIApplication.shared.delegate as! AppDelegate 
17

Swift 4,0

let appDelegate = UIApplication.shared.delegate as! AppDelegate 
let aVariable = appDelegate.value 

Swift 3.0

let appDelegate = UIApplication.shared.delegate as! AppDelegate 
let aVariable = appDelegate.someVariable 

Swift 2,0

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
let aVariable = appDelegate.someVariable 
0
func appDelegate() -> AppDelegate { 
    return UIApplication.sharedApplication().delegate as! AppDelegate 
}