Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NSBundle bundleIdentifier optional?

Tags:

ios

swift

Just wondering why NSBundle bundleIdentifier is optional.

enter image description here

Is there a reason it could be nil?

like image 512
RameshVel Avatar asked Nov 01 '16 15:11

RameshVel


People also ask

What is NSBundle in iOS?

A representation of the code and resources stored in a bundle directory on disk.

What is a valid bundle identifier?

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 (.).

What is bundle ID in Swift?

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.


3 Answers

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

like image 147
Craig Siemens Avatar answered Sep 18 '22 15:09

Craig Siemens


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.

like image 45
Bob Avatar answered Sep 17 '22 15:09

Bob


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:

enter image description here

If you are seeing different key names, right click and choose "Show Row Keys/Values".

In this case, it should be nil.

like image 43
Ahmad F Avatar answered Sep 18 '22 15:09

Ahmad F