2

AutoMapper for .Net 允许您从一种类型映射到另一种类型。它最基本的功能是通过复制类型 A 中存在于类型 B 中的属性值(具有匹配的名称和类型),从另一种类型的类中创建一种类型的类。

例子:

public class ClassA {
    public string StringProp { get; set; }
    public int IntProp { get;set; }
}
public class ClassB {
    public string StringProp { get; set; }
    public int SomeIntProp { get; set; }
}

ClassA classAInstance = new ClassA { StringProp = "Test", IntProp = 5 };
ClassB classBInstance = Mapper.Map<ClassA, ClassB>(classAInstance);

// This creates a new instance of ClassB and sets its StringProp property to "Test".
// It does not set the property on ClassB called "SomeIntProp" because there is no
// property on ClassA called "SomeIntProp"

Objective-C 有类似的东西吗?

4

3 回答 3

1

我一直在寻找同样的东西。

我遇到了这个:

https://github.com/dchohfi/KeyValueObjectMapping

它适用于一个非常常见的用例:JSON 服务器对域模型的响应。

我也刚刚遇到这个:https ://github.com/jwillis/CHAutoMapper

于 2013-04-22T23:50:27.007 回答
1

如果您真的非常想这样做,您可以为此使用键值编码,但我会强烈考虑为什么您可能首先想要做这样的事情。

要使用 Key-Value Coding 来做到这一点,请使用-dictionaryWithValuesForKeys:and-setValuesForKeysWithDictionary:来做到这一点。它们记录在NSKeyValueCoding Protocol Reference中。

于 2010-10-21T05:12:42.210 回答
0

在使用 AutoMapper 后,我遇到了来自 .NET 世界的类似问题,我最终使用了OCMapper库。

特征:

  • 支持数组映射
  • 支持树结构映射
  • 支持复杂对象嵌套
  • 支持核心数据(NSManagedObjects)
  • 映射配置既可以在代码中完成,也可以通过 PLIST 完成
  • 自动根据 NSDictionary 键检测键/值
  • 完全可配置
  • 不需要子类化或向模型添加任何额外代码
  • 自动日期转换和可配置的 DateFormatters
于 2014-06-05T08:00:05.983 回答