从 NSDictionary 传递的 iOS 数据为 (null) - JSON 源

Posted

技术标签:

【中文标题】从 NSDictionary 传递的 iOS 数据为 (null) - JSON 源【英文标题】:iOS Data passed along from NSDictionary is (null) - JSON source 【发布时间】:2015-03-12 06:39:37 【问题描述】:

好的,所以我使用 push segue 从我的CatViewController -> TopicViewController 中分离出来,我将 JSON 数据拉入 CVC 中的字典,并在 UITableView 中打印出来。一旦用户选择了一行,它应该从选定的NSDictionary 对象中提取categoryID,并使用一组不同的 JSON 数据填充下一个视图,其中replyID == categoryID

显然不是这样,当我打印出我的passedData NSString 时,它显示为空。

我以为我正确构建了NSDictionary,然后获得了正确的信息,但它似乎并没有按照我的意愿去做。

CatViewController.m

- (void) retrieveData;

    NSURL *url = [NSURL URLWithString:getDataURL];
    NSData *data = [NSData dataWithContentsOfURL:url];

    NSLog(@"%@", url);

    jsonArray2 = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    NSError *error;
    dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    for (catDict in dict) 
        catID = catDict[@"categoryID"];
        NSString *catDesc = catDict[@"categoryDesc"];
        NSString *catName = catDict[@"categoryName"];
        NSLog(catID);
    

    categoryArray = [[NSMutableArray alloc] init];

    for(int i = 0; i < jsonArray2.count; i++)
    
        NSString *cID = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryID"];
        NSString *cDesc = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryDesc"];
        NSString *cName = [[jsonArray2 objectAtIndex:i] objectForKey:@"categoryName"];

        [categoryArray addObject:[[Categories alloc]initWidthCategoryDesc:cDesc andcategoryName:cName andcategoryID:cID]];
    

    [self.tableView reloadData];


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if ([segue.identifier isEqualToString:@"catToTopic"]) 
        TopicViewController *tVC = (TopicViewController *)[segue destinationViewController];
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        tVC.passedData = [[[categoryArray objectAtIndex:indexPath.section] valueForKey:@"categoryID"] objectAtIndex:indexPath.row];
    

TopicViewController.m

- (void)viewDidLoad

    [super viewDidLoad];

    //test to see what gets passed along
    NSLog(@"%@", passedData);

    self.title = @"Test replies";

    [self retrieveData];

    UITableViewController *tvController = [[UITableViewController alloc] init];
    tvController.tableView = self.tableView;

    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(updateTable:) forControlEvents:UIControlEventValueChanged];
    tvController.refreshControl = self.refreshControl;


输出

2015-03-12 02:29:25.036 Aviato[3081:195303] Login SUCCESS
2015-03-12 02:29:25.314 Aviato[3081:195303] http://tandemenvoy.michaeldvinci.com/forum/categoriesJSON.php
2015-03-12 02:29:25.315 Aviato[3081:195303] 2
2015-03-12 02:29:25.315 Aviato[3081:195303] 5
2015-03-12 02:29:25.315 Aviato[3081:195303] 6
2015-03-12 02:29:25.316 Aviato[3081:195303] 7
2015-03-12 02:29:25.316 Aviato[3081:195303] 8
2015-03-12 02:29:25.316 Aviato[3081:195303] 9
2015-03-12 02:29:25.316 Aviato[3081:195303] 10
2015-03-12 02:29:25.316 Aviato[3081:195303] 11
2015-03-12 02:29:25.317 Aviato[3081:195303] 12
2015-03-12 02:29:25.317 Aviato[3081:195303] 13
2015-03-12 02:29:25.317 Aviato[3081:195303] 14
2015-03-12 02:29:25.317 Aviato[3081:195303] 15
2015-03-12 02:29:27.052 Aviato[3081:195303] (null)
2015-03-12 02:29:27.328 Aviato[3081:195303] http://tandemenvoy.michaeldvinci.com/forum/repliesJSON.php

【问题讨论】:

indexpath.section 和 indexpath.row 的值是多少?尝试使用硬编码值传递它\ 你能发布jsonArray2吗? 2015-03-12 03:13:42.600 Aviato[3170:202855] ( categoryDe​​sc = "尝试实际类别"; categoryID = 2; categoryName = TestCat; , categoryDe​​sc = " kjnk,m"; categoryID = 5; categoryName = qwert; , categoryDe​​sc = hi; categoryID = 6; categoryName = trewq; , categoryDe​​sc = m; categoryID = 7; categoryName = retrievethis;, categoryDe​​sc = j; categoryID = 8; categoryName = qwqwqw; (太长了) 你试过我的答案了吗? 【参考方案1】:

试试这个代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    NSString *categoryIDClicked = [categoryArray objectAtIndex:indexPath.row];
    NSLog("category ID value = %@", categoryIDClicked);

如果您想将 categoryIDClicked 用于另一个类,您必须使用全局变量,然后您可以在项目中的任何位置使用。

【讨论】:

2015-03-12 03:04:25.578 Aviato[3131:200764] 类别 ID 值 = 2015-03-12 03:04:25.622 Aviato[3131:200764] ( null) 所以看起来它只是获取指向它的指针,所以我没有得到正确的东西 - 即使我的 UITableView 填充正确?? 首先你应该打印数组。检查它是否应该包含值 array: 03:13:42.600 Aviato[3170:202855] ( categoryDe​​sc = "尝试实际类别"; categoryID = 2; categoryName = TestCat; , categoryDe​​sc = "kjnk,m"; categoryID = 5; categoryName = qwert; , categoryDe​​sc = hi; categoryID = 6; categoryName = trewq; , categoryDe​​sc = m; categoryID = 7; categoryName = retrievethis;, categoryDe​​sc = j; categoryID = 8; categoryName = qwqwqw; (太长了) 我希望将 categoryID 传递给我的 TopicViewController,因此我可以使用第二个 .JSON 并使用 (replyTopic == categoryID) 填充 TPC 在 JSON 中没有数组以及类别 ID,它是一个带有字典的数组,因此您只需传递 [[categoryArray objectAtIndex:indexPath.row] valueForKey:@"categoryID"];跨度> 【参考方案2】:

尝试(假设您尝试传递 categoryId)

Categories* cat = [categoryArray objectAtIndex:indexPath.row];
    tVC.passedData = cat.categoryID ;

改为

tVC.passedData = [[[categoryArray objectAtIndex:indexPath.section] valueForKey:@"categoryID"] objectAtIndex:indexPath.row];

【讨论】:

【参考方案3】:
tVC.passedData =[categoryArray objectAtIndex:indexPath.row].catID;

只传递这个值。

【讨论】:

对不起! passData 打印为:'2015-03-12 03:21:01.225 Aviato[3190:204280] 类别 ID 值 = ' [categoryArray objectAtIndex:indexPath.row].catID 就用这个

以上是关于从 NSDictionary 传递的 iOS 数据为 (null) - JSON 源的主要内容,如果未能解决你的问题,请参考以下文章

在 Push for Segue [IOS] 中获取 NSDictionary 选定行

iOS 开发,NSDictionary 和 Model 哪个好

无法创建 Plist 文件。 iOS+NSDictionary+JSON

从 NSDictionary 检索键/值对会导致数据不正确并崩溃

ios - 从 NSDictionary 获取值

IOS/Objective-C:从 NSManagedObject 构建 NSDictionary