Search code examples
iosswiftidentifier

use of unresolved identifier (swift)


I am receiving an error when trying to run my project in Xcode. For some reason the "userid" function won't build and I am fairly new to programming so I have no clue as to how to make this work. In simple, the line that's giving me an error is supposed to allow a user to log into their instagram acccount to use the application. I am getting an error that reads

use of unresolved identifier "userID"

Here is my code:

      if (result.result.isSuccess) {
      let json = JSON(result.result.value!)

      if (self.loginType == .Instagram) {
        if let accessToken = json["access_token"].string, _ = json["user"]["id"].string {
          print("Logged into Instagram")
          let user = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: self.coreDataStack.context) as! User
          user.userID = userID
          user.accessToken = accessToken
          user.placesType = "Establishment"
          user.uberAccessToken = ""
          self.coreDataStack.saveContext()
          self.performSegueWithIdentifier("unwindToMapView", sender: ["user": user])
        }
      } else if (self.loginType == .Uber) {
        if let accessToken = json["access_token"].string {
          print("Logged into Uber")
          if let fetchRequest = self.coreDataStack.model.fetchRequestTemplateForName("UserFetchRequest") {
            do {
              let results = try self.coreDataStack.context.executeFetchRequest(fetchRequest) as! [User]
              let user = results.first!
              user.uberAccessToken = accessToken
              user.uberAccessTokenExpiryDate = NSDate().dateByAddingTimeInterval((60 * 60 * 24) * 30)
              user.uberMostRecentRequestID = ""
              self.coreDataStack.saveContext()
              self.performSegueWithIdentifier("unwindToUberView", sender: ["user": user])
            } catch {
              self.showAlertWithMessage("Please try again!", title: "Couln't Fetch User", button: "Ok")
              print("Couldn't fetch user")
              self.close()

Solution

  • Your problem lies with

    if let accessToken = json["access_token"].string, _ = json["user"]["id"].string {
    

    You are assigning the user id to _ instead of userID. Try this:

    if let accessToken = json["access_token"].string, userID = json["user"]["id"].string {