有没有类似于 Objective-c 的 AutoMapper 的东西?
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 有类似的东西吗?
AutoMapper for .Net lets you map from one type to another. Its most basic function is to create one type of class from another type of class by copying property values from type A that exist in type B (have matching names and types).
Example:
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"
Is there anything like this for Objective-C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您真的非常愿意,您可以使用键值编码来实现此目的,但我会强烈考虑为什么您可能首先想做这样的事情。
要使用键值编码来执行此操作,请使用
-dictionaryWithValuesForKeys:
和-setValuesForKeysWithDictionary:
来执行此操作。它们记录在 NSKeyValueCoding 协议参考。You can use Key-Value Coding for this if you really, really want to, but I'd consider strongly why you might want to do such a thing in the first place.
To do it with Key-Value Coding, use
-dictionaryWithValuesForKeys:
and-setValuesForKeysWithDictionary:
to do so. They're documented in the NSKeyValueCoding Protocol Reference.我一直在寻找同样的。
我遇到了这个:
https://github.com/dchohfi/KeyValueObjectMapping
它适用于非常常见的情况用例:JSON 服务器对域模型的响应。
我也刚刚遇到了这个: https://github.com/jwillis/CHAutoMapper
I've been looking for the same.
I came across this:
https://github.com/dchohfi/KeyValueObjectMapping
It will work for a pretty common use-case: JSON server response to domain model.
I also just ran across this: https://github.com/jwillis/CHAutoMapper
使用 AutoMapper 后,我在 .NET 世界中遇到了类似的问题,最终我使用了 OCMapper 库。
功能:
I had a similar problem coming from .NET world after using AutoMapper and I ended up using OCMapper library.
Features: