簡體   English   中英

從nsdictionary分配給nsarray的不兼容指針類型

[英]incompatible pointer types assigning to nsarray from nsdictionary

我是iPhone開發的新手,在這里給出的答案非常成功,所以我希望直接獲得幫助。 我正在從plist將數據讀取到tableview中。 該應用程序運行正常,但編譯時收到2條警告。 我知道為什么會收到錯誤,但解決問題一直沒有成功。 盡管此應用程序有效,但我確實很想有效地解決警告。 當我嘗試將NSDictionary更改為NSArray時,警告消失了,但不再填充該表。

任何幫助將不勝感激。

在Delegate .h文件中將Staff和Data定義為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;   

NSArray包含一維項目列表,其中NSDictionary將鍵映射到值。

數組:

[a,b,c]

字典:

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

您可以將數據聲明為NSDictionary *data; 並將其填充為data = [[NSDictionary alloc] initWithContentsOfFile:DataPath];

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

您代碼中的所有內容都表明,人員和數據屬性是NSDictionary實例。 您將它們初始化為字典對象,並將它們引用為字典對象。 為什么然后將它們聲明為NSArray對象?

您應該更改它們的聲明方式,以便它們在您的頭文件中而不是NSArray中為NSDictionary。 在我看來,這是刪除警告的最合乎邏輯的方法。

假設您的“ staff” NSDictionary的內容有一個名為“ Rows”的鍵,其值為NSArray,這仍然應該起作用。 您必須使用空NSArray初始化self.tableDataSource的代碼似乎是多余的,因為您立即用

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

在您的代碼行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM