Appdelegate Method Introduction

Source: Internet
Author: User

//

Appdelegate.swift

Swifydemo

Import UIKit

Import CoreData

@UIApplicationMain

Entry function UIApplication

Class Appdelegate:uiresponder, Uiapplicationdelegate {

Call class

var Swiftdemoclass=swiftdemoclassmodel (). Demofunca (Strb:12)

var Funaa=divideprinta ()

var Window:uiwindow?

Didfinishlaunchingwithoptions starting the latest calling method

Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?)- > Bool {

Override point for customization after application launch.

Gets the Application object.

Uiapplication.sharedapplication ();

return True

}

No activation status phone call in not activated

Func applicationwillresignactive (application:uiapplication) {

Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state.

Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

program into the background

Func Applicationdidenterbackground (application:uiapplication) {

Use the This method to release the shared resources, save user data, invalidate timers, and store enough application state info Rmation to the restore your application to the it is terminated later.

If your application supports background execution, this method is called instead of Applicationwillterminate:when the User quits.

}

Procedures to enter the foreground

Func Applicationwillenterforeground (application:uiapplication) {

Called as part of the transition from the background to the inactive state; Here's can undo many of the changes made on entering the background.

}

Activation status

Func applicationdidbecomeactive (application:uiapplication) {

Restart any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.

}

resource-intensive talk of cleanup routines

Func applicationwillterminate (application:uiapplication) {

Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.

Saves changes in the application ' s managed object context before the application terminates.

Self.savecontext ()

}

MARK:-Core Data Stack

Lazy var applicationdocumentsdirectory:nsurl = {

The directory the application uses to store the Core Data store file. This code uses a directory named "Tony. Swifydemo "In the application ' s documents Application support directory.

Let URLs = Nsfilemanager.defaultmanager (). Urlsfordirectory (. Documentdirectory, Indomains:. Userdomainmask)

return Urls[urls.count-1] as! Nsurl

}()

Lazy var Managedobjectmodel:nsmanagedobjectmodel = {

The managed object model for the application. This property was not optional. It is a fatal error for the application not to be able to find and load its model.

Let Modelurl = Nsbundle.mainbundle (). Urlforresource ("Swifydemo", Withextension: "MOMD")!

Return Nsmanagedobjectmodel (Contentsofurl:modelurl)!

}()

Lazy var persistentstorecoordinator:nspersistentstorecoordinator? = {

The persistent store coordinator for the application. This implementation creates and return a coordinator, has added the store for the application to it. Optional since there is legitimate error conditions, could cause the creation of the store to fail.

Create the Coordinator and store

var coordinator:nspersistentstorecoordinator? = Nspersistentstorecoordinator (ManagedObjectModel:self.managedObjectModel)

Let URL = self.applicationDocumentsDirectory.URLByAppendingPathComponent ("Swifydemo.sqlite")

var error:nserror? = Nil

var Failurereason = "There is an error creating or loading the application ' s saved data."

If coordinator!. Addpersistentstorewithtype (Nssqlitestoretype, Configuration:nil, Url:url, Options:nil, error: &error) = = Nil {

Coordinator = Nil

Report any error we got.

var dict = [String:anyobject] ()

Dict[nslocalizeddescriptionkey] = "Failed to initialize the application ' s saved data"

Dict[nslocalizedfailurereasonerrorkey] = Failurereason

Dict[nsunderlyingerrorkey] = Error

Error = nserror (domain: "your_error_domain", code:9999, Userinfo:dict)

Replace this with code to handle the error appropriately.

Abort () causes the application to generate a crash log and terminate. You should don't use the This function in a shipping application, although it could be useful during.

NSLog ("Unresolved error \ (error), \ (error!. UserInfo) ")

Abort ()

}

Return Coordinator

}()

Lazy var managedobjectcontext:nsmanagedobjectcontext? = {

Returns the managed object context for the application (which are already bound to the persistent store coordinator for The application.) This property is optional since there was legitimate error conditions that could cause the creation of the context to fail .

Let coordinator = Self.persistentstorecoordinator

If coordinator = = Nil {

return Nil

}

var managedobjectcontext = Nsmanagedobjectcontext ()

Managedobjectcontext.persistentstorecoordinator = Coordinator

Return Managedobjectcontext

}()

MARK:-Core Data Saving support

Func Savecontext () {

If let MoC = Self.managedobjectcontext {

var error:nserror? = Nil

If Moc.haschanges &&!moc.save (&error) {

Replace This implementation with code to handle the error appropriately.

Abort () causes the application to generate a crash log and terminate. You should don't use the This function in a shipping application, although it could be useful during.

NSLog ("Unresolved error \ (error), \ (error!. UserInfo) ")

Abort ()

}

}

}

}

Appdelegate Method Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.