CFDictionaryGetValue () - PullRequest
       1

CFDictionaryGetValue ()

1 голос
/ 15 мая 2011

Я не смог получить значение для ключа.

У меня есть структура pt (которая является значением) и wTiId, wTiId1 (который является ключом).

Я уверен, что что-то не так, я делаю в следующем коде, ноЯ не мог понять, что это такое.

Timers.h
---------
#import <Foundation/Foundation.h>

struct session {
    int a;
    char c;
}pstruct;

@interface Timers : NSObject {
    unsigned short wTiId;
    unsigned short wTiId1;

}
-(void)timer;
@end


Timers.m
--------

#import "Timers.h"

@implementation Timers
-(id)init
{
    if (self=[super init]) {
    wTiId=71;
    wTiId1=72;
    }
    return self;
}

-(void)dealloc
{
    [super dealloc];
}

-(void)timer
{
struct session* pt = &pstruct;

pt->a=12;
pt->c='L';


CFDictionaryValueCallBacks cbvs = {0,NULL,NULL,NULL,NULL};
CFMutableDictionaryRef cfmdict = CFDictionaryCreateMutable(NULL,0,&kCFTypeDictionaryKeyCallBacks,&cbvs);

NSLog(@"Dict size:%d\n",((CFIndex)CFDictionaryGetCount(cfmdict)));

CFNumberRef tiId = CFNumberCreate(NULL,kCFNumberShortType,&wTiId);
CFNumberRef tiId1 = CFNumberCreate(NULL,kCFNumberShortType,&wTiId1);

CFDictionarySetValue(cfmdict,tiId,pt);
NSLog(@"Dict size:%d\n",((CFIndex)CFDictionaryGetCount(cfmdict)));

CFDictionarySetValue(cfmdict,tiId1,pt);
NSLog(@"Dict size:%d\n",((CFIndex)CFDictionaryGetCount(cfmdict)));

NSLog(@"The value is:%s",(CFDictionaryGetValue(cfmdict,tiId)));

CFRelease(tiId);
CFRelease(tiId1);
}
@end

main.m
------
#import <Foundation/Foundation.h>
#import "Timers.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Timers* time = [[Timers alloc]init];
    [time timer];
    [pool drain];
    return 0;
}

output
------

2011-05-15 14:52:54.857 timer[3511:a0f] Dict size:0
2011-05-15 14:52:54.861 timer[3511:a0f] Dict size:1
2011-05-15 14:52:54.861 timer[3511:a0f] Dict size:2
2011-05-15 14:52:54.862 timer[3511:a0f] The value is:

Я также пытался использовать спецификатор формата "% @". Ничего не печатается, когда вызывается функция CFDictionaryGetValue (). Возвращаемый тип этой функции - const void*.

Ответы [ 2 ]

2 голосов
/ 15 мая 2011

Результат CFDictionaryGetValue - это то, что вы положили в словарь.

Вы должны привести результат CFDictionaryGetValue к правильному типу указателя, чтобы получить доступ к элементам структуры:

struct session *value = (struct session *) CFDictionaryGetValue(cfmdict,tiId);
NSLog(@"The value is %d and %c", value->a, value->c);
1 голос
/ 15 мая 2011

Вы не можете хранить структуры в CFDictionarys, будут работать только указатели на структуры. Вы не можете распечатать содержимое структуры с помощью спецификатора формата %s.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...