有没有类似于 Objective-c 的 AutoMapper 的东西?

发布于 2024-09-28 05:38:53 字数 745 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡離愁欲言轉身 2024-10-05 05:38:53

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

要使用键值编码来执行此操作,请使用 -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.

楠木可依 2024-10-05 05:38:53

我一直在寻找同样的。

我遇到了这个:

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

话少情深 2024-10-05 05:38:53

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

功能:

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

I had a similar problem coming from .NET world after using AutoMapper and I ended up using OCMapper library.

Features:

  • Supports array mapping
  • Supports tree structure mapping
  • Supports complex object nesting
  • Supports Core Data (NSManagedObjects)
  • Mapping configuration can be done both in code or through a PLIST
  • Auto detects key/values based on NSDictionary keys
  • Fully Configurable
  • Does not require subclassing or adding any extra code to your models
  • Auto date conversion, and configurable DateFormatters
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文