Get App Name in Swift

后端 未结 17 2024
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 00:20

How do I get the application name in Swift?

Googling gave me this:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@\"CFBundleName\"];
         


        
17条回答
  •  渐次进展
    2020-12-24 01:06

    I believe this solution is more elegant. What's more, using object(forInfoDictionaryKey:) is encouraged by Apple:

    "Use of this method is preferred over other access methods because it returns the localized value of a key when one is available."

    extension Bundle {
        var displayName: String? {
            return object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
        }
    }
    

    Accessing bundle display name:

    if let displayName = Bundle.main.displayName {
        print(displayName)
    }
    

提交回复
热议问题