2013-03-23 45 views
7

Tôi có một plist và bên trong đó một mảng và sau đó thiết lập các yếu tố từ điển? Làm thế nào tôi có thể lấy dữ liệu từ plist đến mảng của tôi?Lấy dữ liệu từ plist

plist

Làm thế nào tôi có thể nhận được tên danh mục trong một mảng?

+1

Tại sao bạn cần tạo một mảng category_name? Đây là một dữ liệu có cấu trúc tốt. Nếu bạn muốn truy cập nó một cách dễ dàng, hãy thử tạo một lớp mô hình cho category với các thuộc tính categoryName và categoryID. Điều này sẽ dễ dàng hơn. – Anupdas

Trả lời

29

Objective-C

// Read plist from bundle and get Root Dictionary out of it 
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]]; 

// Your dictionary contains an array of dictionary 
// Now pull an Array out of it. 
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:@"catlist"]]; 

// Now a loop through Array to fetch single Item from catList which is Dictionary 
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { 
    // Fetch Single Item 
    // Here obj will return a dictionary 
    NSLog(@"Category name : %@",[obj valueForKey:@"category_name"]); 
    NSLog(@"Category id : %@",[obj valueForKey:@"cid"]); 
}]; 

Swift

// Read plist from bundle and get Root Dictionary out of it 
var dictRoot: [NSObject : AnyObject] = [NSObject : AnyObject].dictionaryWithContentsOfFile(NSBundle.mainBundle().pathForResource("data", ofType: "plist")) 
// Your dictionary contains an array of dictionary 
// Now pull an Array out of it. 
var arrayList: [AnyObject] = [AnyObject].arrayWithArray((dictRoot["catlist"] as! String)) 
// Now a loop through Array to fetch single Item from catList which is Dictionary 
arrayList.enumerateObjectsUsingBlock({(obj: AnyObject, index: UInt, stop: Bool) -> Void in 
    // Fetch Single Item 
    // Here obj will return a dictionary 
    NSLog("Category name : %@", obj["category_name"]) 
    NSLog("Category id : %@", obj["cid"]) 
}) 

Swift 2.0 Mã

var myDict: NSDictionary? 
    if let path = NSBundle.mainBundle().pathForResource("data", ofType: "plist") { 
     myDict = NSDictionary(contentsOfFile: path) 
    } 
    let arrayList:Array = myDict?.valueForKey("catlist") as! Array<NSDictionary> 
    print(arrayList) 

    // Enumerating through the list 
    for item in arrayList { 
     print(item) 

    } 

Swift 3,0

// Read plist from bundle and get Root Dictionary out of it 
var dictRoot: NSDictionary? 
if let path = Bundle.main.path(forResource: "data", ofType: "plist") { 
    dictRoot = NSDictionary(contentsOfFile: path) 
} 

if let dict = dictRoot 
{ 
    // Your dictionary contains an array of dictionary 
    // Now pull an Array out of it. 
    var arrayList:[NSDictionary] = dictRoot?["catlist"] as! Array 
    // Now a loop through Array to fetch single Item from catList which is Dictionary 
    arrayList.forEach({ (dict) in 
     print("Category Name \(dict["category_name"]!)") 
     print("Category Id \(dict["cid"])") 
    }) 
} 
+0

// Để tìm nạp trực tiếp category_name sử dụng [[arrayList objectAtIndex: 0] valueForKey: @ "category_name"] –

+0

Tôi đã thêm mã cho Swift 2.0. Xin đừng bận tâm đến tôi –

+0

Không sao, ở đây chúng tôi sẽ giúp đỡ và giải quyết vấn đề bằng cách chia sẻ kiến ​​thức của chúng tôi. thx cho những nỗ lực của bạn –

4
  1. Nhận đường dẫn tập tin của bạn từ bó hoặc từ bất kỳ thư mục
  2. Lấy mảng từ điển lấy từ plist điển
  3. Nhận được lưu trữ trong mảng

    NSString *plistFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test.plist"]; 
    
    NSDictionary *list = [NSDictionary dictionaryWithContentsOfFile:plistFilePath]; 
    NSLog(@"%@",list); 
    NSArray  *data = [list objectForKey:@"catlist"]; 
    for(int i=0; i< [data count]; i++) 
    { 
        NSMutableDictionary *details=[data objectAtIndex:i]; 
        NSLog(@"%@",[details objectForKey:@"category_name"]); 
        NSLog(@"%@",[details objectForKey:@"cid"]); 
    
    } 
    
+0

CNTT không hoạt động @bhargavi – Naveen

+0

Tôi biết nó trễ nhưng chỉ nhìn vào bản cập nhật. nó sẽ hoạt động. –

+0

OK. làm thế nào tôi có thể thay đổi màu thanh điều hướng trong bảng phân cảnh? thực sự tôi nhúng vào điều khiển chuyển hướng và tôi muốn thay đổi màu sắc của nó để RGB (20,60,72) bạn có biết ?? Tôi đã thử rằng: self.navigationController.navigationBar.tintColor = [UIColor colorWithRed: 26 màu xanh lá cây: 62 màu xanh: 72 alpha : 0]; nó không hoạt động – Naveen

-1

thêm tệp .plist vào Mục tiêu => Sao chép tài nguyên gói Với câu trả lời ở trên

Các vấn đề liên quan