9

I'm trying to figure out if I should start using the recently added object literals to subscripting dictionaries, arrays and so on. This following post seems to indicate that iOS5 doesn't support them, and I read that the functionality was added in iOS6.

Is that the case? Is anybody using the new literals in iOS5 successfully?

Community
  • 1
  • 1
Alexandr Kurilin
  • 7,685
  • 6
  • 48
  • 76

2 Answers2

11

You can find details about which versions of iOS (and OS X) support which modern Objective-C features in Apple's Objective-C Feature Availability Index.

The NSNumber, NSDictionary and NSArray literals (e.g. @{ @"key": @"value" }) can be deployed to every iOS release, including iOS 5.

The NSDictionary and NSArray subscripting operators (e.g. myArray[7] or myDictionary[@"hello"]) can be deployed to iOS 5.0 and later releases.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I thought that these should be compiler dependant and not os dependant. Weird! – Lefteris Jun 18 '13 at 23:26
  • @Lefteris They are both. You need a compiler that understands the new syntax. Since subscripting uses new messages, you also need a version of the Foundation framework that supports those new messages. (The new literal syntax uses existing messages.) The document I linked lists the compiler version required and the OS version required. But the question only asked about the iOS version, not about the compiler version. – rob mayoff Jun 18 '13 at 23:34
  • I didn't knew it's creating new messages. i thought they where like I mentioned compiler only changes. Thank you. – Lefteris Jun 18 '13 at 23:37
  • [This clang documentation](http://clang.llvm.org/docs/ObjectiveCLiterals.html#subscripting-methods) describes how subscripting is implemented (and much more). – rob mayoff Jun 18 '13 at 23:39
10

If you're using Xcode 4.5 to build your sources, then yes, they're compatible with iOS 5 as long as you use the latest LLVM compiler and you build against iOS SDK 6.0.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    I'm somewhat confused by the terminology here. Is there basically no relationship between an iOS SDK version and the iOS version number itself? Does iOS SDK 6.0 not imply that it compiles for iOS6 or higher? – Alexandr Kurilin Oct 13 '12 at 04:12
  • 4
    @glitch: There are two main components: iOS SDK version, and iOS deployment target version. The SDK version you build against determines what features you can actually use in your nibs, storyboards and code. The deployment target is the (minimum) version of iOS that you want your app to run on. Some features new to iOS 6 won't work on iOS 5 such as auto-layout, but other features like Objective-C literals, while new to iOS SDK 6, are a compiler feature, and so as long as you have the latest compiler you can build them to run on iOS versions older than the SDK, as long as you deploy to them. – BoltClock Oct 13 '12 at 04:14
  • 2
    @glitch: So in this case, if you set your project's Base SDK to Latest iOS (6.0) and its Deployment Target to iOS 5.0, you can compile Obj-C literals while having the added benefit of running on iOS 5.0. – BoltClock Oct 13 '12 at 04:15
  • 1
    Will xcode be generally smart enough to tell me when I'm using a feature that's only supported in iOS6 if I set the target to 5.0? – Alexandr Kurilin Oct 13 '12 at 04:16
  • That depends, really. I'm not sure if it does warn you, but it'll build it anyway, and if you try to run an app that uses auto-layout on iOS 5, or an app that uses storyboards on iOS 4, it will crash outright. – BoltClock Oct 13 '12 at 04:17
  • 2
    No it will not warn you, but there is a way you can get it to warn you. See my answer here: http://stackoverflow.com/a/12633309/1155387 – borrrden Oct 13 '12 at 06:33