Use of undeclared type 'AppDelegate' Swift

Doshipak picture Doshipak · Nov 19, 2014 · Viewed 21.5k times · Source

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.

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.

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

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)

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.

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]

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?


[UPDATE 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.


[UPDATE 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

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

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.

Answer

Ramin picture Ramin · Dec 5, 2014

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.

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.

Clean, rebuild.