[iOS] 從檔案載入NSMutableArray和NSMutableDictionary 的筆記

在iOS開發上

若是從檔案讀取一些資料,想必會用到

  • NSMutableArray
  • NSMutableDictionary

這二個類別

Mutable是可變動的意思,所以建立之後還可以變動

NSMutableArray的話存取方式採用序號來存取值

而NSMutableDictionary採用自訂的鍵值(key)來存取值(value)

 

二者可以互相混用,NSMutableDictionary裡面有NSMutableArray

或是NSMutableArray裡面包含NSMutableDictionary (就像本次範例)

 

使用initWithContentsOfFile: 方法來載入plist

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/initWithContentsOfFile:

NSMutableArray要取得值使用objectAtIndex 方法

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/objectAtIndex:

NSMutableDictionary要取得值使用objectForKey方法

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDictionary/objectForKey:


提外話一下

如果有寫過php的話

他的概念和php的array有一點點類似

http://php.net/manual/en/language.types.array.php

而NSMutableArray類似於

<?php

$array = array("foo", "bar");
var_dump($array);
?>
螢幕輸出:

array(4) { [0]=> string(3) “foo” [1]=> string(3) “bar” }


NSMutableDictionary類似於

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo"
);
var_dump($array);
?>
螢幕輸出:

array(4) { [“foo”]=> string(3) “bar” [“bar”]=> string(3) “foo” }

 


 

建立一個專案,在專案上按右鍵New File:

Screen Shot 2012-03-25 at 上午10.51.49  

新增一個Property List,名叫article.plist

 

article.plist原始碼如下

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

    <array>

        <dict>

            <key>title</key>

            <string>title 01</string>

            <key>content</key>

            <string>content 01</string>

        </dict>

        <dict>

            <key>title</key>

            <string>title 02</string>

            <key>content</key>

            <string>content 02</string>

        </dict>

        <dict>

            <key>title</key>

            <string>title 03</string>

            <key>content</key>

            <string>content 03</string>

        </dict>

    </array>

</plist>

 

 做一個Button出來,綁定TestpList_btn_onClick方法,該方法如下

 

– (IBAction)TestpList_btn_onClick:(id)sender {

   

    // 取得專案中內建的pList路徑

    NSString *path = [[NSBundlemainBundle] pathForResource:@"article"ofType:@"plist"];

    NSLog(@"pList Path: %@n", path);

    // pList中的資料從檔案載入

     NSMutableArray *data_pList = [[NSMutableArrayalloc] initWithContentsOfFile:path];

   

    for(NSInteger i=0;i<[data_pList count]; i++)

    {

        NSIndexPath *indexPath=[NSIndexPathindexPathForRow:i inSection:0];

        NSString *title = [[data_pList objectAtIndex:indexPath.row] objectForKey:@"title"];

        NSString *content = [[data_pList objectAtIndex:indexPath.row] objectForKey:@"content"];

        NSLog(@"Index: %i Title: %@ Content: %@n", i, title, content);

    }

}

 

 然後運行程式,按下按鈕之後,在Log這邊得到以下結果:

Screen Shot 2012-03-25 at 上午10.55.55  


順便補充在iOS上的printf裡的格式跟一般的C語言不同 

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1

string 要使用 %@

integer 要使用 %i 而不是 %d