manageObjectContext failure - ios

Simple app starts with a table view in a navigation controller with an add button.
My problem is here:
func saveName(name: String) {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
I have issue:
'AppDelegate' does not have a member named 'managedObjectContext'
Thanks.

Related

iOS - Calling SceneDelegate method from ViewController

I tried following code to call Scene Delegate func from a VC and getting error
Thread 1: signal SIGABRT
let sceneDelegate = UIApplication.shared.delegate as! SceneDelegate
sceneDelegate.initializeFirstViewController()
Is there any way i can call SceneDelegate func from VC?
UIApplication.shared.delegate is AppDelegate not SceneDelegate You need
if let scene = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {
// to do
}

App.Delegate from ViewController to TabController Swift 5

My code does not work in Swift 5
var myTabBar = self.storyboard?.instantiateViewController(withIdentifier: "myTabBar") as! UITabBarController
var app = UIApplication.shared.delegate as! AppDelegate
app.window?.rootViewController = myTabBar
Value of type AppDelegate has no member window
Because in new Xcode window is moved to SceneDelegate
I'm meeting you more than half-way here, because we really need more details on what's going on and what you're trying to achieve.
But the error is telling you that the AppDelegate doesn't have a variable called window. You probably want the UIApplication object itself, and not its delegate.
Maybe try replacing
var app = UIApplication.shared.delegate as! AppDelegate
app.window?.rootViewController = myTabBar
with
var app = UIApplication.shared
app.keyWindow?.rootViewController = myTabBar

UILabel.text must be used from main thread only [duplicate]

When I using Swift4in Xcode 9 gives me
UIApplication.delegate must be used from main thread only
.... must be used from main thread only
UI API called from background thread Group
Purple Warning.
My codes;
var appDelegate = UIApplication.shared.delegate as! AppDelegate
public var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let prefs:UserDefaults = UserDefaults.standard
var deviceUUID = UIDevice.current.identifierForVendor!.uuidString
Warning line is;
public var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Another warning like this;
let parameters = [
"tel": "\(self.phone.text!)"
] as [String : String]
Gives
UITextField.text must be used from main thread only
Same error again..
How can I fix it ? Any idea ?
You're making this call on a background queue. To fix, try something like…
public var context: NSManagedObjectContext
DispatchQueue.main.async {
var appDelegate = UIApplication.shared.delegate as! AppDelegate
context = appDelegate.persistentContainer.viewContext
}
Although this is a pretty bad way to do this… you're using your App Delegate as a global variable (which we all know is bad!)
You should look at passing the managed object context from view controller to view controller…
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
(window?.rootViewController as? MyViewController)?.moc = persistentContainer.viewContext
}
and so on

Swift: getting AppDelegate instance as a class func results in Thread 1: EXC_BAD_INSTRUCTION

I want to get my AppDelegate reference from a class func in my AppDelegate. Why is this throwing a Thread 1: EXC_BAD_INSTRUCTION?
class func getDelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
I have also tried to move this to another utility class and as a regular func, but getting the same crash.
Is there a way I can access the AppDelegate as a class func instead of having to write
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
in every class?
Declare class function to get appDelegate in AppDelegate class as
class func getDelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
To access and use appDelegate in other class, call
let delegate = AppDelegate.getDelegate()
delegate.printHello()
Create a class as shown below[in a new class file or in existing class file outside the previous class. No need to put it inside AppDelete.swift file]
class Delegate
{
static var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
}
and you can use it as Delegate.appDelegate in any class.

className.type does not have a member called 'appDel'

I have a reference to my app delegate
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate
That works just fine, but this line of code below gives a error:
let context: NSManagedObjectContext = appDel.managedObjectContext
It gives me the following error:
'vctGebruikers.Type' does not have a member named 'appDel'
I declared them right below my class like this:
class vctGebruikers: UIViewController, UITableViewDelegate, UITableViewDataSource {
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
}
The weird thing is when I paste the code in viewDidLoad or in a function the code just works fine. And I can't figure out what the problem is.
How can I solve this error?
EDIT:
I also need to acces context here:
let results: NSArray = context.executeFetchRequest(request, error: nil)
This is what I got working thanks to #Antonio, but now im not able to acces context and appDel
init(coder aDecoder: NSCoder!) {
let appDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate
self.appDel = appDelegate
self.context = appDelegate.managedObjectContext
super.init(coder: aDecoder)
}
In swift you cannot reference self until all class properties have been initialized.
You are implicitly using self in this line:
let context: NSManagedObjectContext = appDel.managedObjectContext
The solution is to initialize them in an initializer, but using something like:
let appDelegate = (UIApplication.sharedApplication().delegate) as AppDelegate
self.appDel = appDelegate
self.context = appDelegate.managedObjectContext
Also read here, a similar question asked a couple days ago on SO.
One method of instantiating the context is to drill down instead of up from AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let vc = window?.rootViewController as? UIViewController {
vc.context = self.managedObjectContext}
`
And in your view controller, tell it to populate dynamically
var context: NSManagedObjectContext!
Also don't forget to call
import CoreData everywhere you are using said objects
One more final method: Use a closure so that your reference will happen at run time:
var context: NSManagedObjectContext {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
return appDelegate.managedObjectContext
}

Resources