Just wondering why NSBundle bundleIdentifier
is optional.
Is there a reason it could be nil?
A representation of the code and resources stored in a bundle directory on disk.
A bundle ID uniquely identifies a single app throughout the system. The bundle ID string must contain only alphanumeric characters (A–Z, a–z, and 0–9), hyphens (-), and periods (.).
The bundleIds resource represents the app's unique identifier that you can register, modify, and delete. You need a bundle ID before you can assign capabilities with the Bundle ID Capabilities resource or create a provisioning profile with the Profiles resource.
If you're just building an executable, running a script, or using the Swift REPL, bundleIdentifier
will be nil.
For example, make a file called test.swift
containing.
import Foundation
print("\(Bundle.main.bundleIdentifier)")
then run it from the terminal with
swift test.swift
it will print nil
I believe it's because NSBundle
has to pull this information from the Info.plist. Remember, the bundleIdentifier
method is there on all NSBundle
instances, not just the main bundle, and other bundles might not be as well-put-together as the main bundle.
Referring to bundleIdentifier:
The bundle identifier is defined by the CFBundleIdentifier key in the bundle’s information property list.
Because there is a key in the app's .plist
file called CFBundleIdentifier
could be null:
If you are seeing different key names, right click and choose "Show Row Keys/Values".
In this case, it should be nil
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With