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

时间:2015-03-12 06:39:38

标签: ios objective-c json nsdictionary segue

好吧,所以我来自CatViewController - > TopicViewController使用push segue,我将JSON数据拖入CVC中的字典中,并以UITableView打印出来。一旦用户选择了一行,就应该从选定的categoryID对象中提取NSDictionary并使用一组不同的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

3 个答案:

答案 0 :(得分:0)

tVC.passedData =[categoryArray objectAtIndex:indexPath.row].catID;

仅传递此值。

答案 1 :(得分:0)

试试此代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *categoryIDClicked = [categoryArray objectAtIndex:indexPath.row];
    NSLog("category ID value = %@", categoryIDClicked);
}

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

答案 2 :(得分:0)

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

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

代替

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