添加Notification Service Extension后Xcode 9.3报词法与预处理错误求助
Hey there, let's dig into this Notification Service Extension issue you're facing. Since removing the extension fixes the build, the problem is almost certainly tied to how the extension target is configured or interacting with your main app. Here are some common fixes to try out:
1. Check Swift/Objective-C Compatibility Settings
- If your main app uses Objective-C but the extension is Swift (or vice versa), make sure you've set up bridging headers correctly:
- For Swift extensions in an Objective-C project, confirm the
Objective-C Bridging Headerpath is set in the extension target's Build Settings (under Swift Compiler - General). - Double-check that all necessary headers are included in the bridging header, and there are no syntax errors in those headers—broken headers here often trigger lex errors.
- For Swift extensions in an Objective-C project, confirm the
- If both targets use Swift, verify the
Swift Language Versionmatches between the main app and extension. Mismatched versions can cause unexpected lexing issues.
2. Inspect Build Settings for Conflicting Flags
- Go to your extension target's Build Settings >
Other Swift Flags(orOther C Flagsfor Objective-C) and compare them to the main app's settings. Sometimes adding an extension introduces conflicting flags (like outdated compiler flags) that lead to lex errors. - Remove any unnecessary flags from the extension—for example, if your main app uses custom preprocessor macros the extension doesn't need, try stripping them from the extension's settings.
3. Check for Naming Collisions
- Ensure no files in your extension share the same name as files in the main app. Naming collisions can cause lex errors during the build process.
- If you're sharing code between the main app and extension, make sure symbols (classes, structs, constants) aren't duplicated. Use namespaces or unique prefixes to avoid conflicts.
4. Verify Framework & Library Links
- When adding the extension, Xcode might have auto-linked incompatible frameworks or missed required ones. Check the extension target's
Link Binary With Librariessection:- Confirm only necessary frameworks are linked—Notification Service Extension only needs the
UserNotificationsframework by default. - If using third-party libraries, ensure they're properly added to the extension target (via CocoaPods with
use_frameworks!or manual linking, and that the library supports app extensions).
- Confirm only necessary frameworks are linked—Notification Service Extension only needs the
5. Clean Derived Data Thoroughly
- A simple clean build might not cut it. Go to
Xcode > Settings > Locations > Derived Data, click the arrow to open the folder, then delete the entire folder for your project. This clears cached build files that could be conflicting with the extension. - Restart Xcode after deleting derived data, then try building again.
6. Validate the Extension's Info.plist
- A malformed Info.plist in the extension can throw unexpected build errors. Verify:
- The
NSExtensiondictionary has the correctNSExtensionPointIdentifierset tocom.apple.usernotifications.service. - There are no typos or missing keys in the Info.plist—even a small syntax error here can cause issues.
- The
If none of these work, try creating a brand new test project, add a Notification Service Extension to it, and see if it builds. If it does, compare the build settings and configuration between the test project and your main project to spot the discrepancy.
内容的提问来源于stack exchange,提问作者Kishore Kumar
相关产品推荐
相关产品推荐

