1

It seems like Swift 4 is implementing different method to (void)setValuesForKeysWithDictionary(), what is appropriate replacement to this function in Swift 4?

I have struct of

struct User {
    let name: String?
    let email: String?
}

The dictionaryOfUsers is coming from data structure which value are presented dynamically based on the number of users in the database:

let dictionaryOfUsers = [{key1: "name1", key2 : "name1@xyz.com"}, {key1 : "name2", key2 : "name2@gmail.com"} ]   
let users = [User]

Now I want to use this function to create my user array to create dictionary in Swift 4:

for dictionary in dictionaryOfUsers {
    let user = User()
    user.setValuesForKeysWithDictionary(dictionary) 

and append to users

    users.append(user)
}     
4

4 回答 4

4

试试这个方法:

func setValuesForKeys(_ keyedValues: [String : Any])

使用给定字典中的值设置接收器的属性,使用它的键来标识属性。默认实现setValue(_:forKey:)为每个键值对调用...

您的代码有许多小问题 - 以及一些主要问题(有关更多信息,请参阅matt answer)。无论如何,我认为这应该完成你所追求的:

import Foundation

class User: NSObject {
    @objc var name: String!
    @objc var email: String!
}

let dictionaryOfUsers = [
    ["name": "name1", "email": "name1@xyz.com"],
    ["name": "name2", "email": "name2@gmail.com"]
]

var users = [User]()
for dictionary in dictionaryOfUsers {
    let user = User()
    user.setValuesForKeys(dictionary) 
    users.append(user)
} 
于 2017-10-07T02:04:42.273 回答
4

问题是您正在尝试在struct上使用 Cocoa 键值编码(这是setValuesForKeys(_:)由来的)。你不能那样做。Cocoa 是 Objective-C;Objective-C 不知道 Cocoa 结构是什么。您需要是从 NSObject 派生的类,并且它的所有属性也需要公开。User@objc@objc

或者,使用新的 Swift 4 键路径功能,它可以通过键访问结构属性。例子:

struct User {
    var name: String?
    var email: String?
}

let arrayOfDicts = [
    [\User.name: "name1", \User.email : "name1@xyz.com"],
    [\User.name : "name2", \User.email : "name2@gmail.com"]
]

for d in arrayOfDicts {
    var user = User()
    for key in d.keys {
        user[keyPath:key] = d[key]!
    }
    print(user)
}
/*
 User(name: Optional("name1"), email: Optional("name1@xyz.com"))
 User(name: Optional("name2"), email: Optional("name2@gmail.com"))
*/
于 2017-10-08T18:12:38.357 回答
1

不用说 Swift 4 没有 setValuesForKeysWithDictionary 方法。此外,当前的 setValuesForKeys 方法在最新 Xcode 版本的 Swift 4 中经常失败。

我的用户对象如下。

import UIKit

class User: NSObject {
    var name: String?
    var email: String?
}

我自己对这个问题的解决方案是手动添加键作为 NSDictionary,如下所示。

let user = User()
user.email = (snapshot.value as? NSDictionary)?["email"] as? String ?? ""
user.name = (snapshot.value as? NSDictionary)?["name"] as? String ?? ""
print(user.email as Any)
print(user.name as Any)

它可以正常工作,没有任何故障。去尝试一下。

于 2018-06-24T07:33:39.957 回答
0

我正在做某事。如果它有帮助... PS:请使用泛型对其进行优化。

  import Foundation
  import CoreData

   enum ModelInitializationResult {
   case none
   case completedWithoutErrors
   case completedWithNullDataErrors
   case completedWithNoSuchKeyErrors
    }

 extension NSManagedObject {

 //Note: For this to work, the keys in the dictionary should be same as attibute name of this class
func initializeModelFromDictionary(dictionary: [String:Any] )  -> ModelInitializationResult{
  var completionResult = ModelInitializationResult.none

  let modelAttributes = self.entity.attributesByName
  let modelAttributeKeys = modelAttributes.keys

  for case let (keyToSet, valueToSet) in dictionary {
    //check for invalid key
      guard modelAttributeKeys.contains(keyToSet) else{
          completionResult = .completedWithNoSuchKeyErrors
          continue;
      }

      //check valueToSetType for Null
      let valueToSetType = type(of: valueToSet)
      guard valueToSetType != NSNull.self else{
          print("ERROR: Found NSNUll for value to set")
          completionResult = .completedWithNullDataErrors
          continue;
      }

      //Check desiredType ; set value for defined types
      let modelAttributeValue = modelAttributes[keyToSet]
      let modelAttributeType = modelAttributeValue?.attributeType

    switch (modelAttributeType!) {
      case .undefinedAttributeType :
          print("Error : undefinedAttributeType")
      case .integer16AttributeType, .integer32AttributeType, .integer64AttributeType :
          if let anInt = valueToSet as? Int{
              self.setValue(anInt, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }
      case .stringAttributeType :
          if let anString = valueToSet as? String{
              self.setValue(anString, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }
      case .booleanAttributeType :
          if let aBool = valueToSet as? Bool{
              self.setValue(aBool, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }
      case .dateAttributeType :
          if let aDate = valueToSet as? Date{
              self.setValue(aDate, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }
      case .binaryDataAttributeType :
          if let aData = valueToSet as? Data{
              self.setValue(aData, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }

      case .transformableAttributeType :// If your attribute is of NSTransformableAttributeType,
          //print(Pretty//printedFunction(), "desiredType : transformableAttributeType")
          if let anObject = valueToSet as? NSObject {
              self.setValue(anObject, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }

      case .doubleAttributeType :
          //print(Pretty//printedFunction(), "desiredType : doubleAttributeType")
          if let anObject = valueToSet as? Double {
              self.setValue(anObject, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }
      case .floatAttributeType :
          //print(Pretty//printedFunction(), "desiredType : floatAttributeType")
          if let anObject = valueToSet as? Float {
              self.setValue(anObject, forKey: keyToSet)
          }
          else{
              //print(Pretty//printedFunction(),"Error  in setting value for key:\(keyToSet) Incompatible type")
          }
      case .decimalAttributeType :
          print("Error: Data type decimalAttributeType Not handled\(String(describing: modelAttributeType))")
      case .objectIDAttributeType:
          print("Error: Data type transformableAttributeType Not handled\(String(describing: modelAttributeType))")
      default :
          print("ERROR : \(String(describing: modelAttributeType))")
      }
  }
  return completionResult
}

func toDictionary() -> [String:Any]     {
    let keys = Array(self.entity.attributesByName.keys)
    let dict = self.dictionaryWithValues(forKeys: keys)
    return dict
}

func printAllValues (){
    for key in self.entity.attributesByName.keys{
        let value: Any? = self.value(forKey: key)
        print("\(key) = \(String(describing: value))")
    }
}

}

于 2018-07-23T13:00:49.830 回答