使用未声明的类型'AppDelegate'Swift

时间:2023-01-23 17:48:17

I've just spent half a day trying to solve next problem. I am testing the CoreData using Swift language. Follow this tutorial everything works fine.

我花了半天时间试图解决下一个问题。我正在使用Swift语言测试CoreData。按照本教程一切正常。

But after titorial I've tried to modify the structure and my code. The 'src' and groups inside it is folders, not just groups created by xCode.

但在考试后,我试图修改结构和我的代码。其中的'src'和组是文件夹,而不仅仅是xCode创建的组。

NSSExpense.swift

NSSExpense.swift

import Foundation
import CoreData

class NSSExpense: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var descr: String
    @NSManaged var value: NSNumber
    @NSManaged var isMonthly: NSNumber
    @NSManaged var payDayInMonth: NSNumber

    class func createInManagedObjectContext(moc: NSManagedObjectContext, name: String, value: Double, payDayInMonth: Int16, isMonthly: Bool, descr: String!) -> NSSExpense {
        let newExpense = NSEntityDescription.insertNewObjectForEntityForName("NSSExpense", inManagedObjectContext: moc) as NSSExpense

        newExpense.name = name
        newExpense.value = NSNumber(double: value)
        newExpense.payDayInMonth = NSNumber(short: payDayInMonth)
        newExpense.isMonthly = NSNumber(bool: isMonthly)

        if let expenseDesctiption = descr {
            newExpense.descr = expenseDesctiption
        } else {
            newExpense.descr = ""
        }

        return newExpense
    }
}

NSSDataManager.swift

NSSDataManager.swift

import UIKit
import CoreData

class NSSDataManager: NSObject {

    class var sharedDataManager: NSSDataManager {
        struct Static {
            static var instance: NSSDataManager?
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Static.token) {
            Static.instance = NSSDataManager()
        }

        return Static.instance!
    }

    lazy var managedObjectContext : NSManagedObjectContext? = {
        // Error at the next line "Use of undeclared type 'NSSAppDelegate'"
        let appDelegate = UIApplication.sharedApplication().delegate as NSSAppDelegate

        if let managedObjectContext = appDelegate.managedObjectContext {
            return managedObjectContext
        } else {
            return nil
        }
    }()

    var expensesInMemory : [NSSExpense] {
        get {
            let fetchRequest = NSFetchRequest(entityName: "NSSExpense")
            if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [NSSExpense] {
                return fetchResults
            } else {
                return [NSSExpense]()
            }
        }
    }

    func addExpenseWithName(name: String, value: Double, payDayInMonth: Int16, isMonthly: Bool, descr: String!) -> NSSExpense {
        return NSSExpense.createInManagedObjectContext(managedObjectContext!, name: name, value: value, payDayInMonth: payDayInMonth, isMonthly: isMonthly, descr: descr?)
    }

}

I've tried to solve this problem different ways: 1) Create new project (Swift main language) and make the same structure again (failed) 2) Create new project (Objective-C main language). So I have the AppDelegate.h and AppDelegate.m. Add it to Swift files using Bridging-Header. The same problem. (failed)

我试图以不同的方式解决这个问题:1)创建新项目(Swift主语言)并再次制作相同的结构(失败)2)创建新项目(Objective-C主语言)。所以我有AppDelegate.h和AppDelegate.m。使用Bridging-Header将其添加到Swift文件中。一样的问题。 (失败)

Really interesting next thing. If I put next code to the ViewController.swift which creates automatically with new project everything works fine. But when I put this code to any other class. I've code this error.

真的很有趣的下一件事。如果我将下一个代码放到ViewController.swift中,它会自动创建新项目,一切正常。但是当我把这个代码放到任何其他类时。我编码了这个错误。

lazy var managedObjectContext : NSManagedObjectContext? = {
        // Error at the next line "Use of undeclared type 'NSSAppDelegate'"
        let appDelegate = UIApplication.sharedApplication().delegate as NSSAppDelegate

        if let managedObjectContext = appDelegate.managedObjectContext {
            return managedObjectContext
        } else {
            return nil
        }
    }()

[UPDATE 1]

[更新1]

I've tried to create another class right at the same folder as NSSAppDelegate.swift and now everything works fine. However it's still an issue, how can I use the classes which stored in other folders?

我试图在与NSSAppDelegate.swift相同的文件夹中创建另一个类,现在一切正常。但是它仍然是一个问题,我如何使用存储在其他文件夹中的类?


[UPDATE 2]

[更新2]

Just tried to do the same thing in other project. If the file structure is like this so the AppDelegate.swift and NGDataManager.swift are in the same folder everything works great. BUT, if I put the NGDataManager.swift inside the 'src' folder like this (not just group, folder) the error occurs. May be I should create the other question for this.

刚尝试在其他项目中做同样的事情。如果文件结构是这样的,那么AppDelegate.swift和NGDataManager.swift在同一个文件夹中,一切都很好。但是,如果我将NGDataManager.swift放在'src'文件夹中(不仅仅是组,文件夹),则会发生错误。也许我应该为此创建另一个问题。


[UPDATE 3]

[更新3]

I don't know how, but you can forget all I've said in UDATE 2. Because Now thats all not work. I even create a new project with CoreData named "Test" and just create a new class named "TestClass". The magic is in the next thing: if I put this code inside the TestClass.swift

我不知道如何,但你可以忘记我在UDATE 2中所说的一切。因为现在这一切都行不通。我甚至用CoreData创建了一个名为“Test”的新项目,只创建一个名为“TestClass”的新类。接下来就是魔术:如果我把这段代码放在TestClass.swift中

import UIKit

class TestClass: NSObject {
    func someFunc() {
        let appDel = UIApplication.sharedApplication().delegate as AppDelegate
    }
}

the error occurs. But if I put this line in viewDidLoad in ViewController.swift which was generated automatically by xCode

发生错误。但是如果我把这行放在ViewController.swift中的viewDidLoad中,这是由xCode自动生成的

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let appDel = UIApplication.sharedApplication().delegate as AppDelegate
    }
}

their is no error and everything works great. I don't know what to say...

他们没有错误,一切都很好。我不知道该说些什么...

You may see the AppDelegate code here but I didn't modify anything generated automatically by xCode. I've created a Single View Application with this settings.

您可以在此处看到AppDelegate代码,但我没有修改xCode自动生成的任何内容。我用这个设置创建了一个单视图应用程序。

7 个解决方案

#1


46  

Chances are when you created your project you also created a '{ProjectName}Tests' target. The problem is AppDelegate is not assigned membership in the '{ProjectName}Tests' target.

有可能在您创建项目时,您还创建了“{ProjectName}测试”目标。问题是AppDelegate未在“{ProjectName}测试”目标中分配成员身份。

Select AppDelegate.swift then in the right-hand inspector click on the File Inspector (the paper icon) then make sure in the "Target Membership" both your project and the test target checkmarks are set to ON.

选择AppDelegate.swift,然后在右侧检查器中单击File Inspector(纸张图标),然后确保在“Target Membership”中将项目和测试目标复选标记设置为ON。

Clean, rebuild.

清洁,重建。

#2


9  

If trying to access an Objective-C AppDelegate in Swift code, make sure you have #import "AppDelegate.h" in your bridging header file.

如果尝试在Swift代码中访问Objective-C AppDelegate,请确保在桥接头文件中有#import“AppDelegate.h”。

This had me stuck for about an hour :-/

这让我陷入了大约一个小时: - /

#3


3  

I had a similar problem that was resolved when I did a "Clean" option in the Product menu

当我在“产品”菜单中执行“清理”选项时,我遇到了类似的问题

#4


2  

Make sure the Target Memberships are set the same in each file. Target memberships can be found in the properties inspector when you click on a specific file.

确保每个文件中的目标成员资格设置相同。单击特定文件时,可以在属性检查器中找到目标成员身份。

#5


2  

It suppose to be like this.

它假设是这样的。

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

#6


0  

Instead of:

代替:

let appDelegate = UIApplication.sharedApplication().delegate as NSSAppDelegate

try:

尝试:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

#7


0  

Ran into this issue in Xcode 8 and this was the first result when I searched on google so adding my solution.

在Xcode 8中遇到这个问题,这是我在谷歌上搜索所以添加我的解决方案时的第一个结果。

If your project has UI and Unit Tests, make sure your AppDelegate is added to both target memberships. Once I added it to my Tests I was able to access any variables I had in my AppDelegate without having to use UIApplication.shared regardless of how my files were grouped.

如果您的项目具有UI和单元测试,请确保将AppDelegate添加到两个目标成员资格。一旦我将它添加到我的测试中,我就可以访问我在AppDelegate中的任何变量而无需使用UIApplication.shared,无论我的文件是如何分组的。

To check, go to your AppDelegate, click on the file inspector, and look at Target Membership.

要检查,请转到AppDelegate,单击文件检查器,然后查看目标成员身份。

#1


46  

Chances are when you created your project you also created a '{ProjectName}Tests' target. The problem is AppDelegate is not assigned membership in the '{ProjectName}Tests' target.

有可能在您创建项目时,您还创建了“{ProjectName}测试”目标。问题是AppDelegate未在“{ProjectName}测试”目标中分配成员身份。

Select AppDelegate.swift then in the right-hand inspector click on the File Inspector (the paper icon) then make sure in the "Target Membership" both your project and the test target checkmarks are set to ON.

选择AppDelegate.swift,然后在右侧检查器中单击File Inspector(纸张图标),然后确保在“Target Membership”中将项目和测试目标复选标记设置为ON。

Clean, rebuild.

清洁,重建。

#2


9  

If trying to access an Objective-C AppDelegate in Swift code, make sure you have #import "AppDelegate.h" in your bridging header file.

如果尝试在Swift代码中访问Objective-C AppDelegate,请确保在桥接头文件中有#import“AppDelegate.h”。

This had me stuck for about an hour :-/

这让我陷入了大约一个小时: - /

#3


3  

I had a similar problem that was resolved when I did a "Clean" option in the Product menu

当我在“产品”菜单中执行“清理”选项时,我遇到了类似的问题

#4


2  

Make sure the Target Memberships are set the same in each file. Target memberships can be found in the properties inspector when you click on a specific file.

确保每个文件中的目标成员资格设置相同。单击特定文件时,可以在属性检查器中找到目标成员身份。

#5


2  

It suppose to be like this.

它假设是这样的。

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

#6


0  

Instead of:

代替:

let appDelegate = UIApplication.sharedApplication().delegate as NSSAppDelegate

try:

尝试:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

#7


0  

Ran into this issue in Xcode 8 and this was the first result when I searched on google so adding my solution.

在Xcode 8中遇到这个问题,这是我在谷歌上搜索所以添加我的解决方案时的第一个结果。

If your project has UI and Unit Tests, make sure your AppDelegate is added to both target memberships. Once I added it to my Tests I was able to access any variables I had in my AppDelegate without having to use UIApplication.shared regardless of how my files were grouped.

如果您的项目具有UI和单元测试,请确保将AppDelegate添加到两个目标成员资格。一旦我将它添加到我的测试中,我就可以访问我在AppDelegate中的任何变量而无需使用UIApplication.shared,无论我的文件是如何分组的。

To check, go to your AppDelegate, click on the file inspector, and look at Target Membership.

要检查,请转到AppDelegate,单击文件检查器,然后查看目标成员身份。