2

I have seen in many places over the net and even in apple documentation when an array is represented in the following format:

@[obj1,obj2]

For eg; In predicate programming guide there is a statement like this:

NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[greaterThanPredicate, lessThanPredicate]];

But when I write the same in code , i get an 'unexpected @ in program' (as expected) compiler error. Is this just a way of representing arrays or am i missing something?

jscs
  • 63,694
  • 13
  • 151
  • 195
Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • Make sure you are using Xcode 4.4 or later and the latest LLVM compiler (not gcc). – rmaddy Mar 07 '13 at 18:13
  • @JoshCaswell I love your ability to find duplicates. But in this case, I think this one is off. The question indicates that he knows what the syntax does (creates an NSArray). The question is why isn't it working with `NSCompoundPredicate andPredicateWithSubpredicates:`. – rmaddy Mar 07 '13 at 18:16
  • Thanks for the compliment, @rmaddy. Let me explain my thinking: the first sentence of the accepted answer on the question I've linked specifies the required Xcode version. It's got approximately the same information that dasblinkenlight's here answer does. – jscs Mar 07 '13 at 18:18
  • I did a search for this and couldn't find any. Even the related questions couldn't help. Anyway thanks. – Rakesh Mar 07 '13 at 18:19
  • @JoshCaswell OK - I missed the Xcode version reference in the accepted answer. Keep up the stellar duplicate finding. – rmaddy Mar 07 '13 at 18:21
  • @Rakesh: There's generally no reproach intended with a duplicate proposal; it's just an attempt to keep related information all in one place instead of scattered across lots of differenct pages. Also, it's helpful to mention closely-related questions that you've already seen and found unhelpful (and why) in your question. – jscs Mar 07 '13 at 18:31
  • @JoshCaswell: Yup. Didn't think otherwise. Although my search key words were not right. That was the problem. :) – Rakesh Mar 07 '13 at 18:34
  • Also relevant: https://developer.apple.com/library/mac/releasenotes/ObjectiveC/ObjCAvailabilityIndex/ – Peter Hosey Mar 08 '13 at 05:57
  • @JoshCaswell: Whats to do when its been marked as duplicate by multiple people. Should I delete this? The solution is found useful by many though. – Rakesh Mar 08 '13 at 07:11
  • No, there's no need at all for you to delete it. Duplicates are fine to keep around (unless there's _dozens_). – jscs Mar 08 '13 at 07:21

1 Answers1

11

This is a relatively new syntax, it is available only with Xcode that includes clang 3.3 or newer.

This

@[greaterThanPredicate, lessThanPredicate]

is logically equivalent* to this:

[NSArray arrayWithObjects:greaterThanPredicate, lessThanPredicate, nil]

You can always replace the new syntax with the old one without losing functionality.

EDIT (in response to a comment by Nikolai Ruhe) Apple has a different version scheme than the open source version. The correct version numbers that introduced the feature are: Apple 4.0, clang 3.1.


* Under the hood the array initializer of the new syntax is transformed to a call of arrayWithObjects:count:. Thanks to newacct for the correction.
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 3
    clang version numbers are a little confusing: Apple has a different version scheme than the open source version. The correct version numbers that introduced the feature are: Apple 4.0, clang 3.1 – Nikolai Ruhe Mar 07 '13 at 18:16
  • Note: (1) Xcode 4.4 is the earliest version which supports these Objective-C literals. (2) If you build for iOS 5 or earlier, you should not use literals with mutable sets. – Aaron Brager Mar 07 '13 at 18:18
  • 2
    "is equivalent to this:" Not quite. It's equivalent to a call to `arrayWithObjects:count:`. – newacct Mar 07 '13 at 20:10
  • @newacct Thanks for the correction, I fixed the answer to reflect this. – Sergey Kalinichenko Mar 07 '13 at 20:18