Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

NSDictionary⇔自作オブジェクトに変換する方法

More than 5 years have passed since last update.

Appleのサンプルコードで発見したNSDictionaryと自作オブジェクトの相互変換のやり方です。

  • NSDictionaryのデータを自作オブジェクトに変換するのが
    - (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;

  • 自作オブジェクトのデータをNSDictionaryに変換するのが
    - (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;

サンプル

Person.h
#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (strong, nonatomic) NSString *bloodType;
@property (strong, nonatomic) NSNumber *age;

- (NSArray *)personKeys;

@end

Person.m
#import "Person.h"

@implementation Person

- (NSArray *)personKeys
{
    return @[@"firstName", @"lastName", @"bloodType", @"age"];
}

@end

ViewController
#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@property (strong, nonatomic) NSArray *personData;

@end

@implementation ViewController

- (NSArray *)personData
{

    NSMutableArray *persons = [NSMutableArray array];
    NSDictionary *person1 = @{@"firstName": @"Muneo",
                       @"lastName": @"Suzuki",
                       @"bloodType": @"B",
                       @"age": @65};

    NSDictionary *person2 = @{@"firstName": @"Ichiro",
                              @"lastName": @"Suzuki",
                              @"bloodType": @"B",
                              @"age": @39};

    NSDictionary *person3 = @{@"firstName": @"An",
                              @"lastName": @"Suzuki",
                              @"bloodType": @"B",
                              @"age": @26};



    [persons addObject:person1];
    [persons addObject:person2];
    [persons addObject:person3];

    return [persons copy];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Person *person = [[Person alloc] init];
    NSUInteger i = 1;
    for (NSDictionary *personDic in self.personData) {
        [person setValuesForKeysWithDictionary:personDic];
        NSLog(@"%d番目", i);
        NSLog(@"firstName:%@", person.firstName);
        NSLog(@"lastName:%@", person.lastName);
        NSLog(@"bloodType:%@", person.bloodType);
        NSLog(@"age:%@", person.age);

        NSDictionary *personDict = [person dictionaryWithValuesForKeys:[person personKeys]];
        NSLog(@"personDict:%@", personDict);
        i++;
    }

}

@end

出力ログ

1番目
firstName:Muneo
lastName:Suzuki
bloodType:B
age:65
personDict:{
age = 65;
bloodType = B;
firstName = Muneo;
lastName = Suzuki;
}

2番目
firstName:Ichiro
lastName:Suzuki
bloodType:B
age:39
personDict:{
age = 39;
bloodType = B;
firstName = Ichiro;
lastName = Suzuki;
}

3番目
firstName:An
lastName:Suzuki
bloodType:B
age:26
personDict:{
age = 26;
bloodType = B;
firstName = An;
lastName = Suzuki;
}

まとめ

  • NSDictionaryのデータを自作オブジェクトに変換する場合はNSDictionaryのkeyと同じ名前のプロパティを自作オブジェクトで定義してあげること。

  • 自作オブジェクトからNSDictionaryに変換する場合はkeyとなる文字列を配列で返すメソッドを自作オブジェクトに定義すること。

特にJSONデータを処理する際に重宝しそうですね。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away