如何在 iOS 中添加和获取 .plist 中的值

发布于 2024-09-27 02:24:56 字数 82 浏览 2 评论 0原文

我正在实现一个基于网络服务的应用程序。因为我需要在 .plist 中添加一个字符串作为属性,并且只要在代码中需要,我就需要从 .plist 中获取值。

I am implementing a application based on web services. In that I need to add a string as property in .plist and I need to get the value from the .plist whenever I need in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

死开点丶别碍眼 2024-10-04 02:24:56

这是一个代码示例:

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];

Here is a code sample:

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];
明月松间行 2024-10-04 02:24:56
NSBundle* mainBundle = [NSBundle mainBundle]; 

 

// Reads the value of the custom key I added to the Info.plist
NSString *value = [mainBundle objectForInfoDictionaryKey:@"key"];

//Log the value
NSLog(@"Value = %@", value);

// Get the value for the "Bundle version" from the Info.plist
[mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"];

// Get the bundle identifier
[mainBundle bundleIdentifier];
NSBundle* mainBundle = [NSBundle mainBundle]; 

// Reads the value of the custom key I added to the Info.plist
NSString *value = [mainBundle objectForInfoDictionaryKey:@"key"];

//Log the value
NSLog(@"Value = %@", value);

// Get the value for the "Bundle version" from the Info.plist
[mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"];

// Get the bundle identifier
[mainBundle bundleIdentifier];
Bonjour°[大白 2024-10-04 02:24:56
NSURL *url = [[NSBundle mainBundle] URLForResource:@"YOURPLIST" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];

NSLog(@"Here is the Dict %@",playDictionariesArray);

或者你也可以使用以下

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sample.plist"];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"YOURPLIST" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];

NSLog(@"Here is the Dict %@",playDictionariesArray);

or you can use following also

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sample.plist"];
过潦 2024-10-04 02:24:56

从 plist 获取非常简单。

NSString *path = [[NSBundle mainBundle] pathForResource:@"SaveTags" ofType:@"plist"];
if (path) {
    NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:path];
}

如果你想向 plist 添加一些东西,也许你可以在这里找到答案:
如何在plist中写入数据?

但是如果你只想在您的应用程序中保存一些消息,NSUserDefaults 是更好的方法。

Get from plist is very simple.

NSString *path = [[NSBundle mainBundle] pathForResource:@"SaveTags" ofType:@"plist"];
if (path) {
    NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:path];
}

If you want to add something to a plist, maybe you can find the answer here:
How to write data in plist?

But if you only want save some message in you app, NSUserDefaults is the better way.

淡淡の花香 2024-10-04 02:24:56

你不能这样做。任何捆绑包(无论是 iOS 还是 Mac OS)都是只读的,您只能读取它,不能创建文件、写入文件或对捆绑包中的文件执行任何操作。这是苹果安全功能的一部分。您可以使用 NSDocumentsDirectory 来编写和阅读应用程序所需的内容

You can not do this. Any Bundle wether it is iOS or Mac OS is readonly, you can only read to it and you can't create files, write or do anything with the files in a bundle. This is part of the Security features of apple. You can use the NSDocumentsDirectory to writr and read your stuff you need for your app

青瓷清茶倾城歌 2024-10-04 02:24:56

斯威夫特,

我知道这是 12 年前提出的问题。但这是通过谷歌提出的第一个SO问题。因此,为了节省每个人的时间,以下是如何在 swift 中执行此操作:

struct Config {

    // option 1
    static var apiRootURL: String {
        guard let value  = (Bundle.main.object(forInfoDictionaryKey: "BASE_URL") as? String), !value.isEmpty else {
            fatalError("Base URL not found in PLIST")
        }
        return value
    }

    // option 2
    static var databaseName: String {
        guard let value  = (Bundle.main.infoDictionary?["DB_NAME"] as? String), !value.isEmpty else {
            fatalError("DB NAME not found in PLIST")
        }
        return value
    }
    
}

请注意,这两个函数使用略有不同的方法来访问 plist。但实际上它们几乎是一样的。

理论上可能没有plist。因此 infoDictionary 是可选的。但在这种情况下,第一个方法也会返回一个意外的值,从而导致错误。

Apple 指出的一个实际差异:

参考 Bundle.main.object(forInfoDictionaryKey: "BASE_URL")

使用此方法优于其他访问方法,因为当键可用时它会返回键的本地化值。

Swift

I know this was asked 12+ years ago. But this was the first SO question to come up via google. So to save time for everyone, here is how to do this in swift:

struct Config {

    // option 1
    static var apiRootURL: String {
        guard let value  = (Bundle.main.object(forInfoDictionaryKey: "BASE_URL") as? String), !value.isEmpty else {
            fatalError("Base URL not found in PLIST")
        }
        return value
    }

    // option 2
    static var databaseName: String {
        guard let value  = (Bundle.main.infoDictionary?["DB_NAME"] as? String), !value.isEmpty else {
            fatalError("DB NAME not found in PLIST")
        }
        return value
    }
    
}

Notice the 2 functions use slightly diffrent methods to access the plist. But in effect they are almost the same.

In theory there might not be a plist. Hence infoDictionary is optional. But in this case the first method will also return an unexpected value, resulting in an error.

One actual difference as noted by Apple:

Refering to Bundle.main.object(forInfoDictionaryKey: "BASE_URL")

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文