静态成员'load'不能用于'AppDelegate'类型的实例]]

问题描述 投票:0回答:2

有人可以解释为什么我不能在load中使用自定义application(_, didFinishLaunchingWithOptions:)函数吗?

我收到错误:

静态成员'load'不能在'AppDelegate'类型的实例上使用

[当我将函数重命名为func loader(...)并用loader("data.json")调用它时,它可以正常工作。 Xcode不能正确识别我正在使用名为load的自定义函数而不是NSObject.load()函数吗?我知道我可以重命名该功能,但我想了解其根本原因。

AppeDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var data: Maturity? = nil

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        data = load("data.json") // Static member 'load' cannot be used on instance of type 'AppDelegate'
        return true
    }
    ...
}

Data.swift

import Foundation

func load<T: Decodable>(_ filename: String) -> T {
    let data: Data

    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
        else {
            fatalError("Couldn't find \(filename) in main bundle.")
    }

    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
    }

    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
    }
}

有人可以解释为什么我不能在我的应用程序中使用自定义加载函数(_,didFinishLaunchingWithOptions :)吗?我收到错误:无法在类型'...

ios swift function appdelegate uiapplicationdelegate
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.