2010-06-12 84 views
1

的我使用UITableView - Searching table view從plist中讀取insted的代碼陣列

其非常好的易教程,但我真的有不好的時候儘量從plist中讀取

什麼我沒有改變

- (void)viewDidLoad { 
[super viewDidLoad]; 

//Initialize the array. 
listOfItems = [[NSMutableArray alloc] init]; 

TableViewAppDelegate *AppDelegate = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate]; 
listOfItems = [AppDelegate.data objectForKey:@"Countries"]; 

//Initialize the copy array. 
copyListOfItems = [[NSMutableArray alloc] init]; 

//Set the title 
self.navigationItem.title = @"Countries"; 

//Add the search bar 
self.tableView.tableHeaderView = searchBar; 
searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 

searching = NO; 
letUserSelectRow = YES; 

}

這個我如何讀取我的AppDelegate.m的plist

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

NSString *Path = [[NSBundle mainBundle] bundlePath]; 
NSString *DataPath = [Path stringByAppendingPathComponent:@"Data.plist"]; 

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath]; 
self.data = tempDict; 
[tempDict release]; 


// Configure and show the window 
[window addSubview:[navigationController view]]; 
[window makeKeyAndVisible]; 

}

,這我的plist

<plist version="1.0"> 
<dict> 
    <key>Countries</key> 
    <array> 
     <array> 
      <string>USA</string> 
     </array> 
     <array> 
      <string>UK</string> 
     </array> 
    </array> 
</dict> 
</plist> 

和我得到這個錯誤

*終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是:「* - [NSCFArray objectForKey :]:無法識別的選擇器發送到實例0xe09120'

請幫助我的股票與此

+1

在你的plist中,項目後的項目是做什麼的? – gnasher 2010-06-12 21:18:11

+0

不能運行plist沒有 BoSoud 2010-06-13 15:33:42

回答

0

這會爲成員分配一個新分配的可變數組。

listOfItems = [[NSMutableArray alloc] init]; 

這會泄漏上述數組併爲成員分配一個自動釋放的不可變數組。

listOfItems = [AppDelegate.data objectForKey:@"Countries"]; 

後來,當您訪問listOfItems時,它可能已被釋放。使用此:

[listOfItems addObjectsFromArray:[AppDelegate.data objectForKey:@"Countries"]]; 

這一切都假定[AppDelegate.data objectForKey:@"Countries"]是返回一個數組。鑑於plist,它應該,但值得仔細檢查。

+0

當我使用 [listOfItems addObjectsFromArray:[AppDelegate.data objectForKey:@「Countries」]]; 其運行沒有錯誤,但與空表 – BoSoud 2010-06-13 15:36:00

+0

去plist,listOfItems現在是一個數組的數組。如果您期待一串字符串,請更換plist。這聽起來像是一個新問題。 – drawnonward 2010-06-13 17:40:07

+0

我花了3天試圖找出它仍然不能得到它 所有我需要使用本教程http://www.iphonesdkarticles.com/2009/01/uitableview-searching-table-view.html閱讀來自plist – BoSoud 2010-06-13 19:56:01