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 616833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:22:08+00:00 2026-05-13T18:22:08+00:00

When my app starts, it loops through adding values of random numbers with keys

  • 0

When my app starts, it loops through adding values of random numbers with keys of co-ordinates on a grid to a dictionary. Here is a bit of the code:

[grid setObject:v forKey:k];

K is a string in form “xy” where x and y are single digit integers and V is an NSNumber. Both of these are logged to the console before adding so I know these aren’t the problem. However, despite this code running 49 times (for a 7 x 7 grid) the dictionary is empty at the end.

grid is defined in my header file with:

NSMutableDictionary *grid;

And then I initialised it when the app loads (but I don’t know if I have to do this) using the code:

grid = [[[NSMutableDictionary alloc] init] retain];

This is really confusing me because I have only just started learning Objective-C and I have come from the far more forgiving universe of C# and Python.

Thanks in advance for the help!

  • 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-13T18:22:09+00:00Added an answer on May 13, 2026 at 6:22 pm

    In Objective-C, sending a message to nil (a null reference in the C# world), is both legal and common. The result is nil, and so will often pass silently. Once you wrap your head around this, it’s a very useful tool that makes code a lot cleaner (no checks for null, etc.). So, put a breakpoint at the line where you add an entry to the dictionary. To do so, click in the gutter next to the line in Xcode. Run your application with Breakpoints turned on. When the debugger stops at that line, you can hover the mouse over grid. I suspect you’ll see that it’s nil (0x0). You can also open the run console and type po grid to print gdb’s description of grid. Again, I think you’ll find that it’s nil.

    Without seeing more code, it will be impossible to help you track down why grid is nil.

    On a side note, you don’t need the extra -retain following alloc/init. Read the Memory Management Programming Guide for Cocoa. It will be your friend.

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

Sidebar

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.