Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3318010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:39:18+00:00 2026-05-17T22:39:18+00:00

I am working an existing iPhone app, while need to change the code of

  • 0

I am working an existing iPhone app, while need to change the code of C.

I want to use a dictionary in standard C file, simpily, I can not use NSDictionary, compiler said it is invalid in c99(coz the headers Foundation/Foundation.h and Foundation/NSDictionary.h can not be found), but CFDictionaryRef is available (coz header CoreFoundation/CoreFoundation.h can be found and imported). Then I try to work with CFDictionary, I can not insert the key-value pairs into the dictionary, could any one take a while and have a look at following code? Thanks in advance!!

ps, the output of following code are:

Dict size: 0
Key: text.html, value: 111

which means the key-value pair is not inserted…


CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

printf("Dict size: %d\n", (int)((CFIndex)CFDictionaryGetCount(dict)));

int i = 111;
char c[] = "text.html";

const void *key = &c;
const void *value = &i;

printf("Key: %s, value: %d\n", key, *(int *)value);

CFDictionarySetValue(dict, key, value); 

printf("Added\n");

printf("Dict size: %d\n", (int)((CFIndex)CFDictionaryGetCount(dict)));

value = (int *)CFDictionaryGetValue(dict, key);

printf("Value:  %d\n", *(int *)value);

Thanks for your inputs, following are the working code.


//init dict
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    //insert key-value
int i = 111;
const void *value = &i;
CFDictionaryAddValue(dict, CFSTR("text.html"), CFNumberCreate(NULL, kCFNumberSInt32Type, value));
CFNumberRef valueRef = CFDictionaryGetValue(dict, CFSTR("text.html"));

    //get value based on key
int valuePtr;
CFNumberGetValue(valueRef, kCFNumberSInt32Type, (void *)&valuePtr);
    printf("value: %d\n", valuePtr);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:39:19+00:00Added an answer on May 17, 2026 at 10:39 pm

    A CFMutableDictionaryRef created with &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks as arguments expects Core Foundation types as keys and values because it will try to retain/release them (using CFRetain/CFRelease).

    You should work with CFStringRef and CFNumberRef instead of the plain C types (or alternatively, specify other keyCallBacks and valueCallBacks, possibly NULL).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working on an existing project I suddenly got the following error when trying
I'm working on a system were a user can edit existing objects (Filter domain
I'm working with an existing XML document which has a structure (in part) like
I'm working on an upgrade for an existing database that was designed without any
I am currently working on converting an existing web application to support an additional
I am working in a new part of an existing system. What are the
I am working on implementing Zend Framework within an existing project that has a
I am working on some schema changes to an existing database. I backed up
I'm currently working on a large implementation of Class::DBI for an existing database structure,
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.