user2993422
user2993422

Reputation: 306

use of unresolved identifier error when trying to make instance of a class

Im new to swift and having a simple problem - I made the following class import Foundation

class UserIdWithScore: NSObject {

   let userId: String
   let score: String

    init (userId: String, userScore: String) {
      self.userId = userId
      self.userScore = userScore
      super.init()
    }

then Im trying to make an instance of that class from another class

  class otherClass : SimpleCommand
     var usersIdAndScore = [UserIdWithScore()]

I get the following error - 'use of unresolved identifier 'UserIdWithScore' whats weird is that Xcode help me finish my line for UserIdWithScore .. Any suggestions?

Upvotes: 0

Views: 1041

Answers (3)

André Slotta
André Slotta

Reputation: 14030

it has to be

var usersIdAndScore = [UserIdWithScore]()

Upvotes: 1

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

You don't have a default initializer in your class UserIdWithScore. Instead you are initializing the class with two params (userId: String, userScore: String)

So call, var usersIdAndScore = [UserIdWithScore(userId: "userId_String", userScore: "userscore_string")]

or add the following initializer in your class

init() {
    // perform some initialization here
}

Upvotes: 0

Phillip Mills
Phillip Mills

Reputation: 31016

Your initializer has two parameters. Try something like:

var usersIdAndScore = [UserIdWithScore(userId: "Me", userScore: "5")]

Or create an empty initializer that supplies default values.

Upvotes: 1

Related Questions