Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

NSNotificationCenterのまとめ


NSNotificationCenter Class Reference

より意訳

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.

NSNotificationCenterはブロードキャスティング機能を提供します。中身はnotificationのdispatch tableです。

Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

オブジェクトはaddObserver:selector:name:object:かaddObserverForName:object:queue:usingBlock: を使って
センターに登録してNSNotificationを受け取ることができます。

Each running Cocoa program has a default notification center. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single program. If you want to post a notification to other processes or receive notifications from other processes, use an instance of NSDistributedNotificationCenter.


起動しているプログラムはデフォルトのセンターを持っています。なので一般にはセンターを作らなくてすみます。センターはNSNotificationをプログラムの範囲内でのみ通知できます。もし、他のプロセルに通知したり通知をもらいたい場合はNSDistributedNotificationCenterを使ってみてください。

可変数引数

関数の引数として、そしてそれを他の可変数引数の関数へ投げたい時の例。

void debugFunc = ^(NSString *message, ...){
    va_list args;   //可変引数へのポインタ
    va_start(args, message); // 可変引数へのポインタの開始位置をformatに
    NSString *m = [[[NSString alloc] initWithFormat:message arguments:args] autorelease];
    va_end(args);
    ...

NSStringでのフォーマット

ここを参照 String Format Specifiers


String Programming Guide: String Format Specifiers

ランタイム

導入

ランタイムは次をインポートしておくこと。

#import <objc/runtime.h>

Opaque data type(プライベートデータ型)

C言語における抽象データ型。

クラスの構成要素を表現するOpaque data type

typedef struct objc_method *Method;
typedef struct objc_ivar *Ivar;
typedef struct objc_category *Category;
typedef struct objc_property *objc_property_t;

型のエンコーディング

c char
i int
s short
l long
q long long
C unsigned char
I unsigned int
S unsigned short
L unsigned long
Q unsigned long long
f float
d double
B C++ bool, C99 _Bool
v void
* charcter string (char *)
@ object
# class object(Class)
: method selector(SEL)
[array type] array
{name=type..} structure
(name=type..) union
bnum bit field
^type pointer to type
? unknown type

クラスダンプ

- (void)dampClass:(Class)c
{
    NSString *clasName = NSStringFromClass(c);
    NSLog(@"%@", clasName);
    
    NSLog(@"method");
    {
        unsigned int count = 0;
        Method *methodList = class_copyMethodList(c, &count);
        NSMutableString *m = [NSMutableString string];
        for (int i = 0; i < count; i++) {
            Method method = methodList[i];
            NSString *d = [NSString stringWithFormat:@"%s, %s \n", sel_getName(method_getName(method)), method_getTypeEncoding(method)];
            [m appendString:d];
        }
        NSLog(@"%@", m);
        free(methodList);
    }

    NSLog(@"vars");
    {
        unsigned count;
        Ivar *ivars = class_copyIvarList(c, &count);
        
        NSMutableString *m = [NSMutableString string];
        for(int i = 0 ; i < count ; ++i) {
            Ivar ivar = ivars[i];
            
            NSString *d = [NSString stringWithFormat:@"%s, %s \n", ivar_getName(ivar), ivar_getTypeEncoding(ivar)];
            [m appendString:d];
        }
        
        NSLog(@"%@", m);
        free(ivars);
    }
}

    NSLog(@"%@", m);