views:

172

answers:

2

Hi Guys

am Using UITableView - Searching table view

its really nice easy tutorial but i really have bad time try to read from plist

that what i did change

- (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;

}

and This How i read plist from My AppDelegate.m

- (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];

}

and this my plist

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

and i get this error

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFArray objectForKey:]: unrecognized selector sent to instance 0xe09120'

help please am stock with this

A: 

This assigns a newly allocated mutable array to a member.

listOfItems = [[NSMutableArray alloc] init];

This leaks the above array and assigns an autoreleased, immutable array to the member.

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

Later, when you access listOfItems, it may have been released. Use this:

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

This all assumes that [AppDelegate.data objectForKey:@"Countries"] is returning an array. Given the plist, it should, but it is worth double checking.

drawnonward
When i Use [listOfItems addObjectsFromArray:[AppDelegate.data objectForKey:@"Countries"]];its run without error but with empty table
BoSoud
Going by the plist, listOfItems is now an array of arrays. If you are expecting an array of strings, change the plist. That sounds like a new question though.
drawnonward
i have spend 3 days trying to figure it out still cant get it all what i need to use this tutorial http://www.iphonesdkarticles.com/2009/01/uitableview-searching-table-view.html to read from plist
BoSoud
A: 

did you figure this out yet? I'm having a similar problem trying to use a plist for that tutorial.

thanks

hanumanDev