我正在做一个项目,我从服务器上获取一系列字典,如下所示

[{
    accountAuthorizationType = None;
    accountCategory = Wallet;
    accountClass = wallet;
    accountIdentifier = 0;
    accountRoleExternal = "<null>";
    accountStatusExternal = "<null>";
    accountTypeId = 1;
    attachedToUserDate = "<null>";
    balance = 204;
    ....
    .....
}]

在我的项目中,我有一个SDK文件如下
    @interface MNFAccount : MNFObject

    ///******************************
    /// @name Immutable properties
    ///******************************

    /**
     @abstract The bank identifier of an account.

     @discussion This value is an identifier for the account set by the originating bank. This identifier is used when getting account statements.
     */
    @property (nonatomic,copy,readonly) NSString *accountIdentifier;

    /**
     @abstract The realm identifier of an account.
     */
    @property (nonatomic,copy,readonly) NSString *realmIdentifier;

    /**
     @abstract The account type identifier of an account.

     @discussion This value is an identifier for the type (savings, checking, etc.) of an account.
     */
    @property (nonatomic,strong,readonly) NSNumber *accountTypeId;

    /**
     @abstract The balance of an account.
     */
    @property (nonatomic,strong,readonly) NSNumber *balance;

......
.....
.......
}

类中的属性和dict中的键相同。
现在我想把dict转换成MNFAccount对象,如下所示
      for dict in arrayOfDictOfCard {
            let mnfAccount = dict as? MNFAccount
            print(("mnfAccount is :\(mnfAccount?.accountTypeId)"))
        }

但无法转换。
请建议如何将响应字典转换为MNFAccount对象。
任何想法或建议都很好。

最佳答案

你可以从数组中获取字典并使用

var mnfAccount : MNFAccount;
mnfAccount.setValuesForKeysWithDictionary(keyedValues: dict);

关于objective-c - 如何将字典转换为特定的对象类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49168257/

10-09 08:07
查看更多