-1

I understand that the NotificationCenter has changed, and I have looked up how to change it to the new implementation, using this link: NotificationCenter issue on Swift 3, but i still cannot get mine to work! I am doing an assignment from my class using the class text book and this is my class so far:

//
//  ViewController.swift
//  Persistence
//
//  Created by Skyleguy on 10/31/16.
//  Copyright © 2016 Skyleguy. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var lineFields: [UITextField]!

    override func viewDidLoad() {
        super.viewDidLoad()
        let filePath = self.dataFilePath()
        if (FileManager.default.fileExists(atPath: filePath))
        {
            let array = NSArray(contentsOfFile: filePath) as! [String]
            for i in 0 ..< array.count
            {
                lineFields[i].text = array[i]
            }
        }

        let notificationName = Notification.Name("applicationWillResignActive")
        NotificationCenter.default.addObserver(self, selector: #selector(Persistence.applicationWillResignActive(notification: NSNotification)), name: notificationName, object: nil)
        // Do any additional setup after loading the view, typically from a nib.
    }

    func applicationWillResignActive(notification: NSNotification)
    {
        let filePath = self.dataFilePath()
        let array = (self.lineFields as NSArray).value(forKey: "text") as! NSArray
        array.write(toFile: filePath, atomically: true)
    }
}

after all of this I am still getting an error:

"Module "Persistence" has no member named 'applicationWillResignActive'"

please help!

Community
  • 1
  • 1
skyleguy
  • 979
  • 3
  • 19
  • 35
  • "I understand that the NotificationCenter has changed" Then you understand wrong. It hasn't changed at all. – matt Nov 01 '16 at 21:46

2 Answers2

5

First off, this line is wrong:

let notificationName = Notification.Name("applicationWillResignActive")

The whole point of having Notification.Name is that you use the existing constant, which is .UIApplicationWillResignActive.

Second, your whole expression Persistence.applicationWillResignActive(...) is nonsense. That is not how you form a function reference for #selector. This function is part of self, so just use the function name pure and simple.

So, like this:

NotificationCenter.default.addObserver(self, 
    selector: #selector(applicationWillResignActive), 
    name: .UIApplicationWillResignActive, 
    object: nil)
Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
-1

Your notification is incorrectly named, in Swift 3 this has changed to the following:

NSNotification.Name.UIApplicationWillResignActive

That will give you the correct name for your observer.

Another thing is your selector is incorrect, please try the following:

#selector(ViewController.applicationWillResignActive(notification:))
Wink
  • 187
  • 7
  • Both things you said are wrong. The whole point is that you _don't_ have to say `Notification.Name` because Swift knows that this _is_ a `Notification.Name`. And your `#selector` is no more legal as a function reference than the OP's; it appears to be more of a function declaration. – matt Nov 01 '16 at 22:15
  • @matt I agree, however it is not wrong to write `Notification.Name` either. With the selector I believe it must include `(Notification:)` as the function declaration states so. – Wink Nov 01 '16 at 22:47
  • Then you believe wrong, and in any case that is not what you wrote in your answer. What you have wont compile. That is not a matter of opinion or belief. – matt Nov 01 '16 at 22:56
  • @Wink your answer isn't wrong but it is not following convention and not harnessing the implicit type resolution of Swift – applejack42 Jul 07 '17 at 04:10