【问题标题】:incompatible pointer types assigning to nsarray from nsdictionary从 nsdictionary 分配给 nsarray 的不兼容指针类型
【发布时间】:2011-10-25 18:49:05
【问题描述】:

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

任何帮助将不胜感激。

Staff 和 Data 在 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;   

【问题讨论】:

  • 你解决了吗? @Corey Brown 我遇到了同样的问题

标签: iphone ios xcode nsarray plist


【解决方案1】:

NSArray 保存一维项目列表,其中 NSDictionary 将键映射到值。

数组:

[a, b, c]

字典:

{@"a" = @"第一项", @"b" = @"第二项"}

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

然后您可以使用 [data valueForKey:@"key"] 访问字典中的值

【讨论】:

    【解决方案2】:

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

    你应该改变它们的声明方式,使它们在你的头文件中是 NSDictionary 而不是 NSArray。在我看来,这似乎是删除您的警告的最合乎逻辑的方式。

    假设您的“员工”NSDictionary 的内容有一个名为“Rows”的键,其值为 NSArray,这应该仍然有效。您必须使用空 NSArray 初始化 self.tableDataSource 的代码似乎是多余的,因为您立即用

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

    代码中的一行

    【讨论】:

    • 非常感谢。我之前试过这个,桌子空了,但这次它奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    相关资源
    最近更新 更多