从nsdictionary分配给nsarray的不兼容指针类型

时间:2011-10-25 18:49:06

标签: iphone ios xcode nsarray plist

我是iPhone开发的新手,并且从这里获得了很大的成功,所以我希望能直接获得帮助。我正在从plist中将数据读入tableview。应用程序工作正常但我编译时收到2个警告。我知道为什么我会得到错误,但我没有成功解决问题。虽然这个应用程序工作,我真的想有效地解决警告。当我尝试将NSDictionary更改为NSArray时,警告消失但表格不再填充。

非常感谢任何帮助。

员工和数据在Delegate .h文件中定义为NSArray。警告显示在下面的委托.m文件中。

我的代表有以下内容:

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

在我的员工ViewController中,我有以下内容:

    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;   

2 个答案:

答案 0 :(得分:1)

NSArray包含一个项目的一维列表,其中NSDictionary将键映射到值。

  

阵列:

     

[a,b,c]

     

字典:

     

{@“a”= @“第一项”,@“b”= @“第二项”}

您可以将数据声明为NSDictionary *data;并将其填充为data = [[NSDictionary alloc] initWithContentsOfFile:DataPath];

然后使用[data valueForKey:@"key"]

访问字典中的值

答案 1 :(得分:0)

代码中的所有内容都表明人员和数据属性是NSDictionary实例。您将它们初始化为字典对象,并将它们作为字典对象引用。为什么然后将它们声明为NSArray对象?

您应该更改它们的声明方式,因此它们是头文件中的NSDictionary而不是NSArray。在我看来,这是删除警告的最合理方式。

假设你的“staff”NSDictionary的内容有一个名为“Rows”的键,其值为NSArray,这仍然可以工作。使用空NSArray初始化self.tableDataSource所需的代码似乎是多余的,因为您立即用

覆盖该值
self.tableDataSource = [AppDelegate.staff valueForKey:@"Rows"];  

代码中的