84
82

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-08-19

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データを処理する際に重宝しそうですね。

84
82
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
84
82

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?