Search code examples
.netobjective-cautomapperkey-value-coding

Is there anything similar to AutoMapper for 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?


Solution

  • 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.