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.createEntityWithDictionary
it 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?
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.