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

  • SEARCH
  • Home
  • 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 8307053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:27:09+00:00 2026-06-08T18:27:09+00:00

I read all about the new Objective-C literals, and used Xcode to convert my

  • 0

I read all about the new Objective-C literals, and used Xcode to convert my old code, but the indexing code didn’t change. I changed it by hand but then it wouldn’t compile. I saw a post that said we have to wait until iOS 6, but I want the indexing NOW!

Is there any solution?

  • 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-08T18:27:10+00:00Added an answer on June 8, 2026 at 6:27 pm

    Well, there is a way to do it! Add the indexing methods as a category to NSArray and NSDictionary, and you can get the feature for most of the classes you’d want it for. You can read up on ObjectiveC literals here. And thanks to James Webster’s solution for @YES and @NO you can use them properly in your projects now too! (the technique)

    1) Create the Interface files

    // NSArray+Indexing.h
    #if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
    @interface NSArray (Indexing)
    - (id)objectAtIndexedSubscript:(NSUInteger)idx;
    @end
    @interface NSMutableArray (Indexing)
    - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
    @end
    // NSDictionary+Indexing.h
    @interface  NSDictionary (Indexing)
    - (id)objectForKeyedSubscript:(id)key;
    @end
    @interface  NSMutableDictionary (Indexing)
    - (void)setObject:(id)obj forKeyedSubscript:(id)key;
    @end
    #endif
    

    2) Create the Implementation files // see edit below before doing this – you can skip this

    // NSArray+Indexing.m
    #if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
    #import "NSArray+Indexing.h"
    @implementation NSArray (Indexing)
    - (id)objectAtIndexedSubscript:(NSUInteger)idx
    {
        return [self objectAtIndex:idx];
    }
    @end
    @implementation NSMutableArray (Indexing)
    - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx
    {
        [self replaceObjectAtIndex:idx withObject:obj];
    }
    @end
    
    // NSMutableDictionary+Indexing.m
    @implementation  NSDictionary (Indexing)
    
    - (id)objectForKeyedSubscript:(id)key
    {
        return [self objectForKey:key];
    }
    @end
    @implementation  NSMutableDictionary (Indexing)
    - (void)setObject:(id)obj forKeyedSubscript:(id)key
    {
        [self setObject:obj forKey:key];
    }
    @end
    #endif
    

    3) Add the Interface files to your pch file for global use, or add them as needed to .m files

    // Add to PCH file
    #ifdef __OBJC__
        #import <UIKit/UIKit.h>
        #import <Foundation/Foundation.h>
    ...
    #if !defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
    
    // New Indexing
    #import "NSDictionary+Indexing.h"
    #import "NSArray+Indexing.h"
    
    // Provided by James Webster on StackOverFlow
    #if __has_feature(objc_bool) 
    #undef YES 
    #undef NO 
    #define YES __objc_yes 
    #define NO __objc_no 
    #endif 
    
    #endif
    #endif
    
    #endif
    

    4) Rebuild, then add the below files to verify that it all works

    // Test Example
    {
    
        NSMutableArray *a = [NSMutableArray arrayWithArray:@[ @"a", @"b", @"c" ]];
        NSLog(@"%@", a[1]);
        a[1] = @"foo";
        NSLog(@"a: %@", a);
    
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{ @"key" : @"object" }];
        NSLog(@"%@", dict[@"key"]);
    
        dict[@"key"] = @"New Object";
        dict[@"newKey"] = @"WOW a new object";
    
        NSLog(@"dict: %@", dict);
    
        NSLog(@" %@ %@", @YES, @NO );
    }
    

    EDIT: Well, according to a key llvm/clang Apple engineer, there is a library that already gets linked in with the implementations, so you just need the interface file:

    Date: Mon, 20 Aug 2012 15:16:43 -0700
    From: Greg Parker
    To: …
    Subject: Re: How to make Obj-C collection subscripting work on iOS 5?
    …

    As an experiment I added the category @interface for these methods, but not the @implementation — the app still ran fine (at least in the 5.1 simulator)

    The compiler emits the same calls. The magic is in the increasingly inaccurately named libarclite (“It’s Not Just For ARC Anymore™”), which adds implementations of the subscripting methods at runtime if they don’t already exist.

    IIRC there are some subscript-able classes that libarclite does not upgrade (NSOrderedSet, maybe?) so you still need to test thoroughly on older deployment targets.

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

Sidebar

Related Questions

I've read some questions on SO about denying cross domain requests but all just
I've read all posts about routing and Zend Documentation but I still can't solve
First of all, I am a a very new Objective C/Cocoa iOS Developer but
I have read all these articles about how to make system.web.routing work but all
I'm new to Objective-C, and I've read some memory management articles but am having
I have read all threads about this without finding any answer to my problem.
Ive read about it and to be honest it all seems like a bunch
I read some articles about Comet tech. All of them mentioned that the long-life
I have read a lot about soft deletes and archive and saw all the
In informatics theory I hear and read about high-level and low-level languages all time.

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.