Tôi đang triển khai một ứng dụng dựa trên các dịch vụ web. Trong đó tôi cần phải thêm một chuỗi như là thuộc tính trong .plist và tôi cần lấy giá trị từ .plist bất cứ khi nào tôi cần trong mã. Các bạn có thể giúp tôi về điều này không?cách thêm và nhận các giá trị từ .plist trong iphone sdk
18
A
Trả lời
29
Đây là một mẫu mã:
NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];
15
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];
5
NSURL *url = [[NSBundle mainBundle] URLForResource:@"YOURPLIST" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
NSLog(@"Here is the Dict %@",playDictionariesArray);
hoặc bạn có thể sử dụng sau cũng
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sample.plist"];
2
Lấy từ plist rất đơn giản.
NSString *path = [[NSBundle mainBundle] pathForResource:@"SaveTags" ofType:@"plist"];
if (path) {
NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:path];
}
Nếu bạn muốn thêm một cái gì đó để một plist, có thể bạn có thể tìm thấy câu trả lời ở đây: How to write data in plist?
Nhưng nếu bạn chỉ muốn tiết kiệm một số tin nhắn trong bạn ứng dụng, NSUserDefaults là cách tốt hơn.
0
Bạn không thể thực hiện việc này. Bất kỳ gói nào không phải là iOS hoặc Mac OS là chỉ đọc, bạn chỉ có thể đọc nó và bạn không thể tạo tệp, viết hoặc làm bất cứ điều gì với các tệp trong một gói. Đây là một phần của tính năng bảo mật của quả táo. Bạn có thể sử dụng NSDocumentsDirectory để viết và đọc nội dung bạn cần cho ứng dụng
có thể trùng lặp của [nhận dữ liệu từ plist tới NSMutableArray và viết NSMutableArray cho cùng một plist iPhone] (http://stackoverflow.com/questions/9193363/get-data-from-plist-to-nsmutablearray-and-writing-the-nsmutablearray-to-same) – Monolo
Xem [liên kết này] (http://www.iphoneexamples.com/) .Hy vọng điều này sẽ rất hữu ích cho bạn. Tất cả là tốt nhất. – Warrior