不兼容的指针类型将NSDictionary分配给NSArray类型的参数

时间:2015-04-28 01:07:02

标签: ios objective-c casting nsarray nsdictionary

我正在尝试将字典转换为objective-c中的数组。我在第三行得到了这个警告。 '不兼容的指针类型将nsdictionary分配给nsarray类型的参数'。有没有办法在没有得到警告的情况下将该字典转换为数组?

// in DataSource.h

@property (nonatomic,strong) NSArray *bars;

// in MasterViewController.m

       AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


    NSDictionary *JSONDict = (NSDictionary *) responseObject;
    _dataSource.recievedJSON = JSONDict;
    _dataSource.bars = [NSArray arrayWithArray:JSONDict];

    self.title = @"Bars";
    [self.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // display alert if error downlading data
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
    }];

    [operation start];

2 个答案:

答案 0 :(得分:1)

你在说:

[NSArray arrayWithArray:JSONDict]

但是JSONDict是一个NSDictionary,而arrayWithArray:期望一个NSArray。那你为什么这样做呢?

您需要考虑如何将此词典转换为数组 - 如果这是您的目标 - 然后需要这样做。你需要了解字典的结构,并考虑什么样的"数组"这可能会变成。

答案 1 :(得分:0)

您无法将NSDictionary(键值对)转换为NSArray,但如果您想获取字典的键数组和值,那么可以试试这个。

Private Sub createDot(ByVal x As Integer, ByVal y As Integer)

    MsgBox(x.ToString & " " & y.ToString)
    Dim myGraphics As Graphics = Panel.CreateGraphics

    Dim myPen As Pen

    myPen = New Pen(Drawing.Color.Maroon, 1)

    myGraphics.DrawRectangle(myPen, x, y, 1, 1)

End Sub

Private Sub Panel1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
    missed += 1
    lblMissed.Text = missed

    createDot(e.x, e.y)
End Sub
相关问题