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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:47:07+00:00 2026-06-01T08:47:07+00:00

I know Apple is big on having you use NS objects instead of true

  • 0

I know Apple is big on having you use NS objects instead of true primitive types, but I need the capabilities of an array (namely direct access of items at indices). However, it seems that they are so very keen on using NS objects that I can’t find a single tutorial online or in a textbook about how to use basic primitive arrays. I want something that does things like this does in Java:

String inventory[] = new String[45];

inventory[5] = "Pickaxe";
inventory[12] = "Dirt";
inventory[8] = "Cobblestone";

inventory[12] = null;

System.out.println("There are " + inventory.length + " slots in inventory: " + java.util.Arrays.toString(inventory));

The following is the closest I’ve gotten in Objective-C, but it won’t run properly:

NSString *inventory[45];

inventory[5] = @"Pickaxe";
inventory[12] = @"Dirt";
inventory[8] = @"Cobblestone";

inventory[12] = nil;

NSArray *temp = [NSArray arrayWithObjects:inventory count:45];
NSLog(@"There are %i slots in inventory: %@", [temp count], [temp description]);

Also, if at all possible, is there something in O-C that will give me the count of non-null/non-nil objects in the array? (This way, I can tell how much space is left in the inventory so that the player can’t pack away anything if it’s full)

  • 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-06-01T08:47:08+00:00Added an answer on June 1, 2026 at 8:47 am

    typically, you would use NSArray/NSMutableArray, although you could also use C arrays.

    NSArray (and most Foundation collections) cannot contain nil entries – you can use NSPointerArray (OS X) if you need nil values, or simply use [NSNull null] in an NSArray to designate nil.

    Here’s your program using an NSArray:

    NSMutableArray * inventory = [NSMutableArray array];
    for (NSUInteger idx = 0; idx < 45; ++idx) {
      [inventory addObject:[NSNull null]];
    }
    
    [inventory replaceObjectAtIndex:5 withObject:@"Pickaxe"];
    [inventory replaceObjectAtIndex:12 withObject:@"Dirt"];
    [inventory replaceObjectAtIndex:8 withObject:@"Cobblestone"];
    
    [inventory replaceObjectAtIndex:12 withObject:[NSNull null]];
    
    NSArray *temp = [NSArray arrayWithObject:inventory];
    NSLog(@"There are %i slots in inventory: %@", [temp count], [temp description]);
    

    Also, if at all possible, is there something in O-C that will give me the count of non-null/non-nil objects in the array?

    An NSArray is a CFArray – they are “toll free bridged”. Use CFArrayGetCountOfValue with [NSNull null] as the value to seek. Most of the CF-APIs are available in the NS-APIs, but not this one. You could easily wrap it in a category, if needed often.

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

Sidebar

Related Questions

Need to know which event triggered after text selection done in Apple iPad. It
First, yes i know this is a big security NONO . But the scenario
I know that apple knows when you touch the locked screen (iPod shortcut, call),
Would anyone one know if Apple has restrictions on providing a user feedback/bug report
Beyond the regular boring difference between Cast and As if i know that apple
I want to know that does apple's Push Notification Server generate new device token
I'm a Windows developer... I know nothing about Apple Mac OS X. In Windows
I know it might not be according to Apple's human interface guidelines for the
Does anyone know what end user information a developer receives from Apple regarding a
How can I know that my iPhone is connected to the Apple Push Notification

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.