skip to Main Content

in Xcode, given a type or a method name, how can I determine the header file(s) to use? For example, if I add either of these lines to my Xcode project

1.  AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
2.  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

the compiler says "Unknown type name AVCaptureDevice" or "Use of undeclared identifier ‘kSystemSoundID_Vibrate’". (That second one needs #import <AudioToolbox/AudioToolbox.h>.) The header files are not listed in Xcode’s documentation files for iOS. Sometimes I get lucky and a code snippet on Stack Overflow will show the header files, but usually the import and include lines are not shown. I always #import <UIKit/UIKit.h>.

How can I find the correct header file(s) to import for a line in Xcode?

2

Answers


  1. There are several approaches, but probably the most fool-proof here would be to lookup AVCaptureDevice in the Xcode docs (Cmd-Shift-0) or at developer.apple.com:

    https://developer.apple.com/search/?q=avcapturedevice
    

    This will take you to the main page:

    https://developer.apple.com/documentation/avfoundation/avcapturedevice/
    

    And that will show you the correct framework: AVFoundation (observe the path to this file, also visible in the Xcode docs). So you want @import AVFoundation.

    From your description, it seems you already found "Audio Toolbox" in the docs. That will pretty reliably be @import AudioToolbox (i.e. just remove the space). I can’t think of any cases where existing docs don’t match the name of the framework. (There are important cases where the Xcode docs are missing entirely, like CommonCrypto, but I can’t think of any where they give the wrong name.)

    Note that the vast majority of Apple headers are now modular, so you can and should use the @import syntax with just the name of the framework, rather than the #import syntax with the path to the header.

    You could also often option-click (for the docs) or command-click (for this header) AVCaptureDevice in the code. This sometimes requires already importing the correct file, but sometimes it doesn’t.

    Some things are more obscure, and might not have an obvious documentation page, and Xcode may have trouble finding it if you don’t know where to look. In that case, you can search the SDK directly.

    Go to the SDK directory you want:

    cd $(xcrun --sdk iphoneos --show-sdk-path)/System/Library/Frameworks
    

    You can than search for all references to the symbol you want. This may not immediately give you the correct framework, but it’s a quick way to narrow things down:

    find . -name '*.h' | xargs grep AVCaptureDevice
    

    Or you might choose to use something like ack or ag to search a bit more nicely.

    Login or Signup to reply.
  2. The Open Quickly command searches all identifiers and filenames in your project and the SDK. You can find Open Quickly in the File menu, but you’ll probably want to use a keyboard shortcut, which by default is ⌘⇧O. (That’s command shift letter-O, which is different than the command shift zero shortcut for the documentation window that @RobNapier mentions in his answer).

    So for example if I start Open Quickly and type ksystemsound, I see this:

    The Open Quickly floating window. I have typed the search string “ksystemsound” and the search results show five entries. The first entry is for kSystemSoundID_Vibrate and shows the path Simulator - iOS 16.2 - AudioToolbox - AudioServices.h:132.

    This tells me that the symbol is defined in the AudioToolbox framework, so I can import it using either the modern @import directive

    // Objective-C
    @import AudioToolbox; // <- must end with a semicolon!
    

    or the older #import directive

    // Objective-C
    #import <AudioToolbox/AudioToolbox.h>
    

    and in Swift I would use the import statement

    // Swift
    import AudioToolbox
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search