0

I'm having a problem, and I can't find the solution, I have an extension of an object Article, which has a func which creates the object with the data contained in a dictionary passed as a parameter to the func, this is the code:

protocol EntityProtocol {

     mutating func createEntityWithDictionary(dictionary: Dictionary<String, AnyObject>)

}


extension Article: EntityProtocol {

  func createEntityWithDictionary(dictionary: Dictionary<String, AnyObject>) {

    var article: Article! = ModelManager.instance.insertNewEntityName("Article") as Article

    for (key: String, value: AnyObject) in dictionary {

        switch key {

        case kContentTypeKey:

            article.contentType = value as String

        case kEditorsPickKey:

            article.editorsPick = value as Bool

        default:

            println("Default")

        }

    }

  }

}

Ok, so in another class, I invoke the func passing a dictionary as parameter, but when I write Article.createEntityWithDictionaryit autocompletes the name of the method, but the type of the parameter is Article instead of Dictionary, and if I pass a dictionary as parameter XCode says "NSDictionary is not a subtype of 'Article'".

What am I missing here?

dandan78
  • 13,328
  • 13
  • 64
  • 78
diegomen
  • 1,804
  • 1
  • 23
  • 37
  • Please try using the new syntax sugar for dictionaries: `[String:AnyObject]` instead of `Dictionary` – Teejay Aug 19 '14 at 07:55
  • done, but still having the error 'NSDictionary' is not a subtype of 'Article'... :( – diegomen Aug 19 '14 at 08:25
  • the thing is that when I write the beginning of the func's name, the autocomplete shows that: ([String:AnyObject]) -> Void createEntityWithDictionary(self. Article) I can't understand why it takes Article type as parameter when I have clearly defined that I want a dictionary.. – diegomen Aug 19 '14 at 08:27

1 Answers1

1

Ok I finally found what was the problem. The thing was that I was trying to access Article like a class instead of like an object, so it couldn't access that method. I solved it changing the line

Article.createEntityWithDictionary(dictionary)

with:

ArticleManager.instance.createArticle().fillEntityWithDictionary(dictionary)

Doing that, when we access the method fillEntityWithDictionary (which was the method createEntityWithDicitonary) before executing it, I create an object Article in the ArticleManager, which is a singleton which manages all the actions relatives to objects of the type Article. After having a valid object, there's no problem in executing fillEntityWithDictionary.

diegomen
  • 1,804
  • 1
  • 23
  • 37