LoginSignup
0
0

More than 5 years have passed since last update.

Objective-C で用意されたクラスのインスタンス変数を取得する

Posted at
#import <Foundation/Foundation.h>

@interface Foo : NSObject
{
    int age;
}

- (id)init;
@end


@implementation Foo : NSObject
- (id)init
{
    if (self = [super init]) {
        age = 42;
    }
    return self;
}
@end

のような感じで Objective-C コード内で用意されているインスタンス変数 age をどうしても取得したい場合には、

    foo = Foo.alloc.init
    p foo.valueForKey('age')

valueForKey: が使えるようです。

内部実装のためにインスタンス変数が使われているので、不用意に外部から利用すると実装が変わったりするとクラッシュを引き起こすかもしれません。アクセサが用意されていればそれを使いましょう。

valueForKey: が強力すぎるな、という話でした。

0
0
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
0
0