如何将字典转换为特定的对象类型

时间:2018-03-08 08:08:03

标签: objective-c swift

我正在开发项目,我从服务器获取一个字典数组,如下所示

[{
    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;

......
.....
.......
}
课堂上的

属性&amp; dict中的键是一样的。

现在我想将dict转换为MNFAccount对象,如下所示

      for dict in arrayOfDictOfCard {
            let mnfAccount = dict as? MNFAccount
            print(("mnfAccount is :\(mnfAccount?.accountTypeId)"))
        }

但无法转换。

请建议如何将响应字典转换为MNFAccount对象。

任何想法或建议都会很棒。

1 个答案:

答案 0 :(得分:3)

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

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