title: Swift error: unrecognized selector sent to instance along with unknown method / function
body: <p>I'm new to swift and i have no idea why the app is terminating. I'm using a class function to save data to the persistent store. I also don't have a function called setYearOfBirth. can someone please explain what is going on</p>
  
  <p>Here is the error</p>
  
  <pre>
  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[User setYearOfBirth:]: unrecognized selector sent to instance 0x600000462a40'
  *** First throw call stack:
  (
      0   CoreFoundation                      0x000000010e01f12b __exceptionPreprocess + 171
      1   libobjc.A.dylib                     0x000000010d213f41 objc_exception_throw + 48
      2   CoreFoundation                      0x000000010e0a0024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
      3   CoreFoundation                      0x000000010dfa1f78 ___forwarding___ + 1432
      4   CoreFoundation                      0x000000010dfa1958 _CF_forwarding_prep_0 + 120
      5   Vings                               0x000000010a7fa081 _T05Vings7SetUpVCC9buildUserAA0F0CyF + 833
      6   Vings                               0x000000010a7fb101 _T05Vings7SetUpVCC14saveBtnPressedyypF + 2177
      7   Vings                               0x000000010a7fb648 _T05Vings7SetUpVCC14saveBtnPressedyypFTo + 72
      8   UIKit                               0x000000010ebb1972 -[UIApplication sendAction:to:from:forEvent:] + 83
      9   UIKit                               0x000000010ed30c3c -[UIControl sendAction:to:forEvent:] + 67
      10  UIKit                               0x000000010ed30f59 -[UIControl _sendActionsForEvents:withEvent:] + 450
      11  UIKit                               0x000000010ed2fe86 -[UIControl touchesEnded:withEvent:] + 618
      12  UIKit                               0x000000010f1a1bad _UIGestureEnvironmentSortAndSendDelayedTouches + 5560
      13  UIKit                               0x000000010f19ba4d _UIGestureEnvironmentUpdate + 1506
      14  UIKit                               0x000000010f19b41f -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
      15  UIKit                               0x000000010f19a4cb -[UIGestureEnvironment _updateGesturesForEvent:window:] + 288
      16  UIKit                               0x000000010ec28f14 -[UIWindow sendEvent:] + 4102
      17  UIKit                               0x000000010ebcc365 -[UIApplication sendEvent:] + 352
      18  UIKit                               0x000000010f518a1d __dispatchPreprocessedEventFromEventQueue + 2809
      19  UIKit                               0x000000010f51b672 __handleEventQueueInternal + 5957
      20  CoreFoundation                      0x000000010dfc2101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
      21  CoreFoundation                      0x000000010e061f71 __CFRunLoopDoSource0 + 81
      22  CoreFoundation                      0x000000010dfa6a19 __CFRunLoopDoSources0 + 185
      23  CoreFoundation                      0x000000010dfa5fff __CFRunLoopRun + 1279
      24  CoreFoundation                      0x000000010dfa5889 CFRunLoopRunSpecific + 409
      25  GraphicsServices                    0x00000001132289c6 GSEventRunModal + 62
      26  UIKit                               0x000000010ebb05d6 UIApplicationMain + 159
      27  Vings                               0x000000010a7f0ea7 main + 55
      28  libdyld.dylib                       0x00000001118bcd81 start + 1
      29  ???                                 0x0000000000000001 0x0 + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException
  </pre>
  
  <p>Here is the code:</p>
  
  <pre><code>class userAction{
      private class func save(completion: (_ finished: Bool) -&gt; (), user: User){
          do{
              try managedContext.save()
              completion(true)
          }catch{
              debugPrint("could not save \(error.localizedDescription)")
              completion(false)
          }
      }
  }
  </code></pre>
  
  <p>The following is in a view controller class:</p>
  
  <pre><code>func buildUser() -&gt; User{
      let user = User(context: managedContext)
      user.yearOfBirth = Int32(ageTxtField.text!)!
      user.wage = Double(wageTxtField.text!)!
      user.gender = userGender.rawValue
      user.hoursWorked = Int32(hoursWorkedTxtField.text!)!
      user.savingsAmount = 0.001
      return user
  }
  
  @IBAction func saveBtnPressed(_ sender: Any) {
      if(ageTxtField.text != "") &amp;&amp; (wageTxtField.text != "") &amp;&amp; (userGender != .blank) &amp;&amp; (hoursWorkedTxtField.text != ""){
          let user = buildUser()
          userAction.save(completion: { (complete) in
              AuthService.instance.anonymousSignUp(userCreationComplete: { (isComplete, error) in
                  if complete{
                      self.performSegue(withIdentifier: "toRevealVC", sender: self)
                  }
              }, passedUser: user)
          }, user: user)
      }
  }
  </code></pre>
  
  <p>The following is in its own separate class:</p>
  
  <pre><code>class AuthService{
      static let instance = AuthService()
  
      func anonymousSignUp(userCreationComplete: @escaping(_ status: Bool, _ error: Error?) -&gt; (), passedUser: User){
          Auth.auth().signInAnonymously { (user, error) in
              guard let user = user else{
                  userCreationComplete(false, error)
                  return
              }
              var dataToPost: [String : Any]
              dataToPost = ["Gender" : passedUser.gender!, "Year of birth" : passedUser.yearOfBirth, "Salary" : passedUser.wage, "Hours worked in a day" : passedUser.hoursWorked]
              DataService.instance.createDBUser(uid: user.uid, userData: dataToPost)
              userCreationComplete(true, nil)
          }
      }
  }
  </code></pre>