简体   繁体   中英

incompatible pointer types assigning to nsarray from nsdictionary

I'm new to iPhone development and have had great success with with answers from here so I am hoping to receive help directly. I am reading data into a tableview from a plist. The application works fine but I get 2 warnings when I compile. I know why I get the errors but I have been unsuccessful with resolving the issues. Although this app works I really would like to resolve the warnings efficiently. When I tried changing the NSDictionary to NSArray the warning goes away but the table is no longer populated.

Any help would be greatly appreciated.

Staff and Data are defined as NSArray in the Delegate .h file. The warnings show in the delegate .m file below.

My Delegate has the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window

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

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


    NSDictionary *staffDict = [[NSDictionary alloc]initWithContentsOfFile:StaffPath];
    **self.staff = staffDict;**  
    [staffDict release];

In my staff ViewController I have the following:

    if(CurrentLevel == 0) {

        //Initialize our table data source
        NSArray *staffDict = [[NSArray alloc] init];
        self.tableDataSource = staffDict;
        [staffDict release];


        Midwest_DigestiveAppDelegate *AppDelegate = (Midwest_DigestiveAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.tableDataSource = [AppDelegate.staff valueForKey:@"Rows"];     
}
else 
    self.navigationItem.title = CurrentTitle;   

An NSArray holds a one dimensional list of items where an NSDictionary maps keys to values.

Array:

[a, b, c]

Dictionary:

{@"a" = @"first item", @"b" = @"second item"}

Could you declare data as NSDictionary *data; and populate it as data = [[NSDictionary alloc] initWithContentsOfFile:DataPath];

You then access values in the dictionary with [data valueForKey:@"key"]

Everything in your code suggests that the staff and data properties are NSDictionary instances. You initialize them to dictionary objects and you reference them as dictionary objects. Why then are you declaring them as NSArray objects?

You should change how they are declared so they are NSDictionary in your header file rather than NSArray. That seems to me the most logical way to remove your warnings.

This should still work assuming the contents of your "staff" NSDictionary has a key named "Rows" whose value is an NSArray. The code you have to initialize self.tableDataSource with an empty NSArray seems redundant, as you immediately overwrite the value with the

self.tableDataSource = [AppDelegate.staff valueForKey:@"Rows"];  

line in your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM