swift cannot subscript a value of type [String: DataModelBase.Type] with an index of 'String' - swift

I'm just starting swift and am trying to replicate functionality I've used with obj-c. basically it's a json to object model mapping. the code I'm using looks like it should work, but I'm getting the error in the title and am not sure why or how to fix it.
Here's some sample code:
struct foo {
let modelMap = ["DataModelBase" : DataModelBase.self,
"ManifestDataModel" : ManifestDataModel.self]
func createModel(classString: String, json: Dictionary<String, AnyObject>) -> AnyObject {
if let modelClass: DataModelBase = modelMap[classString] {
let model = modelClass.initWithDictionary(json)
return model
}
return json
}
The error is being triggered on the line:
if let modelClass: DataModelBase = modelMap[classString]
DataModelBase is a base class and ManifestDataModel subclasses DataModelBase
Can someone shed some light on why the error message is happening and how I can fix it?

What you want in that if let statement is probably
if let modelClass: DataModelBase = modelMap[classString] as? DataModelBase {
this will only run if modelMap[classString] is a DataModelBase
EDIT:
class DataModelBase {
required init(json: [String:AnyObject]) {
}
}
class ManifestDataModel: DataModelBase {
required init(json: [String:AnyObject]) {
super.init(json: json)
}
}
struct foo {
let modelMap = ["DataModelBase" : DataModelBase.self,
"ManifestDataModel" : ManifestDataModel.self]
func createModel(classString: String, json: [String: AnyObject]) -> AnyObject {
if modelMap[classString] != nil {
if let modelType = modelMap[classString] {
let model = modelType.init(json: json)
return model
}
}
return json
}
}

Related

Swift Generic parameter 'T' could not be inferred

I have a generic dictionary, and built a generic function to access that dictionary (to prevent concurrency access problems).
My (singleton) data class looks like:
class AppData {
static let genericDict: Dictionary<String, Any> = [:]
static func genericGet<T: Any>(_ objID: String) -> T {
let retVal: T
mySemaphore.wait()
retVal = genericDict[objID] as! T
mySemaphore.signal()
return retVal
}
}
However, when I call my function like so:
class SomeClass {
let product: SomeObj = AppData.genericGet(objID) as! SomeObj
}
I get the error:
Generic parameter 'T' could not be inferred
I both explicitly and implicitly cast the type to my desired value. Not sure what else I can do to fix this issue.
I've tried restarting XCode, does not help.
The real code:
public class AppData: ObservableObject {
static private var dataDirectory: Dictionary<String, Any> = Dictionary(minimumCapacity: 10000)
static let dataGetLock = DispatchSemaphore(value: 1)
static func get<T: Any>(_ objID: String) -> T? {
let retVal: T
dataGetLock.wait()
retVal = dataDirectory[objID] as! T
dataGetLock.signal()
return retVal
}
}
I get the error on the following two lines:
class StoreViewEnvironment: ObservableObject {
func initProductLoad(storeID: String) {
...
let liveStore: Store = AppData.get(storeID) as! Store
let liveMenu: Menu = AppData.get(liveStore.menuID) as! Menu
...
}
}
I don't approve of the way you're doing this (on the general grounds that Any is just about always a bad smell), but here's a version that compiles (just delete the Any constraint):
class AppData {
static let genericDict: Dictionary<String, Any> = [:]
static func get<T>(_ objID: String) -> T {
let retVal: T
retVal = genericDict[objID] as! T
return retVal
}
}
class SomeClass {
let product : String = AppData.get("yoho")
}

Classes or Structs for Models in Swift


I want to implement IgListKit for my UICollectionView. This Library requires me to use “Class Model : ListDiffable”
Accoding to my current architecture I have “Struct Model : Decodable” As I use JSON Decoder in my NetworkService to retrieve Data
I have 2 struct, 1 for the Root and the 2 for my Array.
struct Talents : Decodable {
let status : String?
let error : String?
let response : String?
}
struct Talent: Decodable {
let id: String
let name : String
let smallDesc: String
let largeDesc : String
}
\\I also have enum CodingKeys to match the keys for both structs
Following is the Struct output ,Works well to be used in my UICollectionView
when I change these structs to class
class Talents : Decodable {
var status : String?
var error : String?
var response : String?
init( status : String,error : String, response : String){
self.status = status
self.error = error
self.response = response
}
}
This is the Class Output I get, Which I am not sure how to use.
What are the changes I should make in order to resolve this, and apply : ListDiffable Protocol stubs to my Model class?
Service File with the instance of which in viewDidLoad of my CollectionVC I take the Data in an array.
static func getCategoryTalents(category:String,completion: #escaping (Bool, [Talent]?, Error?) -> Void) {
let parameters: Parameters = [
"filter": category,
"shuffle": 1
]
AF.request(Constants.baseUrl,
parameters : parameters ).response { response in
guard let data = response.data else {
DispatchQueue.main.async {
print("\(Error.self)")
completion(false, nil, Error.self as? Error)
}
return}
do {
let talentsResponse = try JSONDecoder().decode(Talents.self, from: data)
print(talentsResponse)
let firstJSONString = talentsResponse.response?.replacingOccurrences(of: "\\", with: "")
let secondJSONString = firstJSONString?.replacingOccurrences(of: "\"{", with: "{").replacingOccurrences(of: "}\"", with: "}")
guard let talentArray = try! JSONDecoder().decode([Talent]?.self, from: (secondJSONString?.data(using: .utf8)!)!) else {
return }
print(talentArray)
var talents = [Talent]()
for talent in talentArray {
guard let individualTalent = talent as Talent? else { continue }
talents.append(individualTalent)
}
DispatchQueue.main.async {
completion(true, talents, nil)
}
} catch {
DispatchQueue.main.async {
completion(false, nil, error)
}
}
}
}
You don't need to change the existing struct to class create a new class and make an initializer which accepts struct as the parameter:
struct TalentDataModel: Decodable {
let status : String?
let error : String?
let response : String?
}
class Talents: Decodable {
var status : String?
var error : String?
var response : String?
init(dataModel: TalentDataModel) {
status = dataModel.status
error = dataModel.error
response = dataModel.response
}
}
Since it is a whole lot of work to make serve Models with srtucts as seen here
I changed my class to make it work with IGListKit.
import Foundation
import IGListKit
class Talents: NSObject,Decodable {
let status : String
let error : String
let response : String
init(status:String,error:String,response:String) {
self.status = status
self.error = error
self.response = response
}
}
extension NSObject: ListDiffable {
public func diffIdentifier() -> NSObjectProtocol {
return self
}
public func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
return isEqual(object)
}
}

Dynamic protocol conformance in Swift

Hi I am struggle to solve the problem dynamic protocol conformance in swift language. Please see code.
Protocol:
protocol Object {
init(by object: [String: Any])
}
Custom structs with protocol object conformance:
struct Tree: Object {
let treeName: String
init(by object: [String: Any]) {
self.treeName = object["tree"] as? String ?? "Notree"
}
}
struct Plant: Object {
let plantName: String
init(by object: [String : Any]) {
self.plantName = object["tree"] as? String ?? ""
}
}
The above code just fine until the object is [String: Any]. I can't use [[String: Any]] like below.
let coconut = ["tree":"Coconut"] // => This fine
let allTrees = [["tree":"Apple"],["tree":"Orange"],["tree":"Jakfruit"]] //=> Here is the problem
let aTree = Tree(by: coconut)
let bTree = Tree(by: ["data":allTrees])
let cTree = Plant(by: ["data":allTrees])
I can't use array of objects. So, I used to store objects in to key "data". Now I used extension: Array confirm protocol object.
extension Array: Object where Element == Object{
init(by object: [String : Any]) {
if let data = object["data"] as? [[String: Any]]{
self = data.map({ (object) -> Object in
// return Plant.init(by: object) // => Works, But I need dynamic confirmance
// return Tree.init(by: object) // => Works, But I need dynamic confirmance
return Object.init(by: object) //=> How can I do?
})
}else{
self = []
}
}
}
The return Object shows error Protocol type 'Object' cannot be instantiated. I tried lot to solve but not able.
Can someone suggest better idea or solution for this problem? Thank you in advance...
First, you should not use the constraint == Object. You want to say that not only [Object] is an Object, but also [Plant] and [Tree] are Objects too, right? For that, you should use the : Object constraint. Second, you can use Element.init to initialise a new Element of the array. Because of the constraint Element : Object, we know that a init(by:) initialiser exists:
extension Array: Object where Element: Object{
init(by object: [String : Any]) {
if let data = object["data"] as? [[String: Any]]{
self = data.map({ (object) in
return Element.init(by: object)
})
}else{
self = []
}
}
}
Usage:
let trees = [Tree](by: ["data": allTrees])
Here's what I think a more Swifty version of your code, making use of failable initialisers - initialisers that return nil when they fail to initialise the object:
protocol Object {
init?(by object: [String: Any])
}
struct Tree: Object {
let treeName: String
init?(by object: [String: Any]) {
if let treeName = object["tree"] as? String {
self.treeName = treeName
} else {
return nil
}
}
}
struct Plant: Object {
let plantName: String
init?(by object: [String : Any]) {
if let plantName = object["tree"] as? String {
self.plantName = plantName
} else {
return nil
}
}
}
extension Array: Object where Element: Object{
init?(by object: [String : Any]) {
if let data = object["data"] as? [[String: Any]]{
self = data.compactMap(Element.init)
}else{
return nil
}
}
}

Swift can't infer generic type when generic type is being passed through a parameter

I'm writing a generic wrapper class for core data.
Here are some of my basic types. Nothing special.
typealias CoreDataSuccessLoad = (_: NSManagedObject) -> Void
typealias CoreDataFailureLoad = (_: CoreDataResponseError?) -> Void
typealias ID = String
enum CoreDataResult<Value> {
case success(Value)
case failure(Error)
}
enum CoreDataResponseError : Error {
typealias Minute = Int
typealias Key = String
case idDoesNotExist
case keyDoesNotExist(key: Key)
case fetch(entityName: String)
}
I've abstracted my coredata writes in a protocol. I'd appreciate if you let me know of your comments about the abstraction I'm trying to pull off.
Yet in the extension I run into the following error:
Cannot convert value of type 'NSFetchRequest' to
expected argument type 'NSFetchRequest<_>'
Not sure exactly how I can fix it. I've tried variations of changing my code but didn't find success...
protocol CoreDataWriteManagerProtocol {
associatedtype ManagedObject : NSManagedObject
var persistentContainer : NSPersistentContainer {get}
var idName : String {get}
func loadFromDB(storableClass : ManagedObject.Type, id: ID) throws -> CoreDataResult<ManagedObject>
func update(storableClass : ManagedObject.Type, id: ID, fields: [String : Any]) throws
func fetch(request: NSFetchRequest<ManagedObject>, from context: NSManagedObjectContext)
init(persistentContainer : NSPersistentContainer)
}
extension CoreDataWriteManagerProtocol {
private func loadFromDB(storableClass : ManagedObject.Type, id: ID) -> CoreDataResult<ManagedObject>{
let predicate = NSPredicate(format: "%# == %#", idName, id)
let fetchRequest : NSFetchRequest = storableClass.fetchRequest()
fetchRequest.predicate = predicate
// ERROR at below line!
return fetch(request: fetchRequest, from: persistentContainer.viewContext)
}
func fetch<ManagedObject: NSManagedObject>(request: NSFetchRequest<ManagedObject>, from context: NSManagedObjectContext) -> CoreDataResult<ManagedObject>{
guard let results = try? context.fetch(request) else {
return .failure(CoreDataResponseError.fetch(entityName: request.entityName ?? "Empty Entity Name")) // #TODO not sure if entityName gets passed or not.
}
if let result = results.first {
return .success(result)
}else{
return .failure(CoreDataResponseError.idDoesNotExist)
}
}
}
Additionally if I change the line:
let fetchRequest : NSFetchRequest = storableClass.fetchRequest()
to:
let fetchRequest : NSFetchRequest<storableClass> = storableClass.fetchRequest()
I get the following error:
Use of undeclared type 'storableClass'`
My intuition tells me that the compiler can't map 'parameters that are types' ie it doesn't understand that storableClass is actually a type. Instead it can only map generics parameters or actual types. Hence this doesn't work.
EDIT:
I used static approach Vadian and wrote this:
private func create(_ entityName: String, json : [String : Any]) throws -> ManagedObject {
guard let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: Self.persistentContainer.viewContext) else {
print("entityName: \(entityName) doesn't exist!")
throw CoreDataError.entityNotDeclared(name: entityName)
}
let _ = entityDescription.relationships(forDestination: NSEntityDescription.entity(forEntityName: "CountryEntity", in: Self.persistentContainer.viewContext)!)
let relationshipsByName = entityDescription.relationshipsByName
let propertiesByName = entityDescription.propertiesByName
guard let managedObj = NSEntityDescription.insertNewObject(forEntityName: entityName, into: Self.persistentContainer.viewContext) as? ManagedObject else {
throw CoreDataError.entityNotDeclared(name: entityName)
}
for (propertyName,_) in propertiesByName {
if let value = json[propertyName] {
managedObj.setValue(value, forKey: propertyName)
}
}
// set all the relationships
guard !relationshipsByName.isEmpty else {
return managedObj
}
for (relationshipName, _ ) in relationshipsByName {
if let object = json[relationshipName], let objectDict = object as? [String : Any] {
let entity = try create(relationshipName, json: objectDict)
managedObj.setValue(entity, forKey: relationshipName)
}
}
return managedObj
}
But the following piece of it is not generic as in I'm casting it with as? ManagedObject. Basically it's not Swifty as Vadian puts it:
guard let managedObj = NSEntityDescription.insertNewObject(forEntityName: entityName, into: Self.persistentContainer.viewContext) as? ManagedObject else {
throw CoreDataError.entityNotDeclared(name: entityName)
}
Is there any way around that?
My suggestion is a bit different. It uses static methods
Call loadFromDB and fetch on the NSManagedObject subclass. The benefit is that always the associated type is returned without any further type cast.
Another change is throwing errors. As the Core Data API relies widely on throwing errors my suggestion is to drop CoreDataResult<Value>. All errors are passed through. On success the object is returned, on failure an error is thrown.
I left out the id related code and the update method. You can add a static func predicate(for id : ID)
protocol CoreDataWriteManagerProtocol {
associatedtype ManagedObject : NSManagedObject = Self
static var persistentContainer : NSPersistentContainer { get }
static var entityName : String { get }
static func loadFromDB(predicate: NSPredicate?) throws -> ManagedObject
static func fetch(request: NSFetchRequest<ManagedObject>) throws -> ManagedObject
static func insertNewObject() -> ManagedObject
}
extension CoreDataWriteManagerProtocol where Self : NSManagedObject {
static var persistentContainer : NSPersistentContainer {
return (UIApplication.delegate as! AppDelegate).persistentContainer
}
static var entityName : String {
return String(describing:self)
}
static func loadFromDB(predicate: NSPredicate?) throws -> ManagedObject {
let request = NSFetchRequest<ManagedObject>(entityName: entityName)
request.predicate = predicate
return try fetch(request: request)
}
static func fetch(request: NSFetchRequest<ManagedObject>) throws -> ManagedObject {
guard let results = try? persistentContainer.viewContext.fetch(request) else {
throw CoreDataResponseError.fetch(entityName: entityName)
}
if let result = results.first {
return result
} else {
throw CoreDataResponseError.idDoesNotExist
}
}
static func insertNewObject() -> ManagedObject {
return NSEntityDescription.insertNewObject(forEntityName: entityName, into: persistentContainer.viewContext) as! ManagedObject
}
}
The issue is that NSManagedObject.fetchRequest() has a return type of NSFetchRequest<NSFetchRequestResult>, which is non-generic. You need to update the definition of your fetch function to account for this. Btw the function signatures of the default implementations in the protocol extension didn't actually match the function signatures in the protocol definition, so those also need to be updated.
You also need to change the implementation of fetch(request:,from:), since NSManagedObjectContext.fetch() returns a value of type [Any], so you need to cast that to [ManagedObject] to match the type signature of your own fetch method.
protocol CoreDataWriteManagerProtocol {
associatedtype ManagedObject : NSManagedObject
var persistentContainer : NSPersistentContainer {get}
var idName : String {get}
func loadFromDB(storableClass : ManagedObject.Type, id: ID) throws -> CoreDataResult<ManagedObject>
func update(storableClass : ManagedObject.Type, id: ID, fields: [String : Any]) throws
func fetch(request: NSFetchRequest<NSFetchRequestResult>, from: NSManagedObjectContext) -> CoreDataResult<ManagedObject>
init(persistentContainer : NSPersistentContainer)
}
extension CoreDataWriteManagerProtocol {
private func loadFromDB(storableClass : ManagedObject.Type, id: ID) -> CoreDataResult<ManagedObject>{
let predicate = NSPredicate(format: "%# == %#", idName, id)
let fetchRequest = storableClass.fetchRequest()
fetchRequest.predicate = predicate
return fetch(request: fetchRequest, from: persistentContainer.viewContext)
}
func fetch(request: NSFetchRequest<NSFetchRequestResult>, from context: NSManagedObjectContext) -> CoreDataResult<ManagedObject> {
guard let results = (try? context.fetch(request)) as? [ManagedObject] else {
return .failure(CoreDataResponseError.fetch(entityName: request.entityName ?? "Empty Entity Name")) // #TODO not sure if entityName gets passed or not.
}
if let result = results.first {
return .success(result)
}else{
return .failure(CoreDataResponseError.idDoesNotExist)
}
}
}

Swift Get all the properties in generic class

I'm trying to get all the members of a generic class T, I can get the properties based on a specific class.
But, how I can do it using Mirror ?
let mirrored_object = Mirror(reflecting: user)
for (index, attr) in mirrored_object.children.enumerated() {
if let propertyName = attr.label as String! {
print("Attr \(index): \(propertyName) = \(attr.value)")
}
}
I added this as extension
extension NSObject {
public func GetAsJson() -> [[String:Any?]] {
var result:[[String: Any?]] = [[String: Any?]]()
for item in self {
var dict: [String: Any?] = [:]
for property in Mirror(reflecting: self).children {
dict[property.label!] = property.value
}
result.append(dict)
}
return result
}
}