xcode UIApplication 没有成员共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43413109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:55:00  来源:igfitidea点击:

UIApplication has no member shared

iosswiftxcode

提问by bircastri

Im'm building an app to iOS system with Swift language. So I have add to my project, the core data.

我正在用 Swift 语言为 iOS 系统构建一个应用程序。所以我在我的项目中添加了核心数据。

I have also created 'CoreDataController'

我还创建了“CoreDataController”

this is the code of it:

这是它的代码:

import Foundation
import CoreData
import UIKit

class CoreDataController {

    static let shared = CoreDataController()

    private var context: NSManagedObjectContext

    private init() {
        let application = UIApplication.shared.delegate as! AppDelegate
        self.context = application.persistentContainer.viewContext
    }

}

At line UIApplication.shared.delegate ..... I have an error

在行 UIApplication.shared.delegate ..... 我有一个错误

Type 'UIApplication' has no member shared

“UIApplication”类型没有共享成员

This is my appdelegade file

这是我的 appdelegade 文件

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        self.saveContext()
    }

    lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
        let container = NSPersistentContainer(name: "ArduinoHomeKit")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

}

I have jump this information of AppDelegate file?

我有跳转 AppDelegate 文件的这些信息吗?

回答by mohsen

use for Xcode 7.3.1 by swift 2.2

Swift 2.2 用于 Xcode 7.3.1

let appvar = UIApplication.sharedApplication().delegate as! AppDelegate

and for swift 3.x, 4.x, 5 (xcode 8.0 and newer)

以及 swift 3.x、4.x、5(xcode 8.0 和更新版本)

let appvar = UIApplication.shared.delegate as! AppDelegate