4

The new syntax in Objective-C in Xcode 5 is @importto import a framework.

See question for details. Advantage is that you don't have to include the framework in project avoiding linker errors, you don't need to add quotes and .h to just the name of the framework, it is faster for precompiled headers, and you have a namespace that protects you from accidentally renaming a symbol. All nice additions.

My question is, for your own files, for example MyFancyViewController.h, do you continue to use #import or does @import completely replace it? Also, can I define my own modules easily? Just looks more messy having both syntaxes in the same file.

Community
  • 1
  • 1
possen
  • 6,778
  • 2
  • 34
  • 43

2 Answers2

6

for you including of your project files do you continue to use #import or does @import completely replace it?

@import, so far, is for Apple frameworks only, so at the time of writing you still have to use #import for anything else.
The good news is that, if you opt-in, any #import will be implicitly replaced for you by the compiler, so you don't need to convert your previous code to benefit from modules.

Also, can I define my own modules easily?

Yes and no.
Yes, it's easy, but...
...no you cannot, since this feature is currently not supported for non-Apple frameworks.

To define your own module - if you could - you would need to do:

export MyAwesomeModule:
public:
   // methods and whatever you want to export
Gabriele Petronella
  • 102,227
  • 20
  • 204
  • 227
  • Nice answer! As usual ;) – HAS Sep 30 '13 at 05:55
  • 2
    The way to create your own modules is using [module maps](http://clang.llvm.org/docs/Modules.html#module-map-language). I haven't actually tried it, but you should be able to use this to create your own modules. – bames53 Oct 01 '13 at 16:22
2

@import is for official frameworks only. For MyFancyViewController.h continue use #import.

Shmidt
  • 15,652
  • 16
  • 79
  • 129
  • 4
    While `@import` is handy, Xcode automatically converts any `#imports` of Apple frameworks to `@imports` at compile time anyway - so you don't have to use the `@import` keyword at all and still get the benefits. – Ephemera Sep 30 '13 at 04:40
  • It doesn't so much convert it as cause #import to behave like @import -- your files remain unchanged. Note that this is only automatic for new projects, too. For existing projects, turn on *Enable Modules* in your build settings. – rickster Sep 30 '13 at 05:26
  • 1
    With iOS 8 it appears that writing your own frameworks is possible so using an @import will work for your own code and is encouraged. – possen Jun 11 '14 at 02:13
  • @possen thank you. I will check it later and update the answer. – Shmidt Jun 11 '14 at 06:40