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

     2023-03-15     129

关键词:

【中文标题】从 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 检索键/值对会导致数据不正确并崩溃

】从NSDictionary检索键/值对会导致数据不正确并崩溃【英文标题】:retrievingkey/valuepairsfromNSDictionaryleadstoincorrectdataandalsocrash【发布时间】:2012-01-1806:56:58【问题描述】:我的字典包含两个键值对日期/文本..这里的日期也是字符串... 查看详情

ios - 从 NSDictionary 获取值

】ios-从NSDictionary获取值【英文标题】:ios-getvaluesfromNSDictionary【发布时间】:2013-09-0811:44:19【问题描述】:我的服务器上有JSON,它被解析为iOS应用程序到NSDictionary。NSDictionary看起来像这样:(text=Aaa;title=1;,text=Bbb;title=2;)我的问题... 查看详情

从 NSDictionary 保存数据到 NSDictionary

】从NSDictionary保存数据到NSDictionary【英文标题】:SavingdatainNSDictionaryfromNSDictionary【发布时间】:2016-11-1620:27:23【问题描述】:这是我已解析并保存在名为NSDictionary*dic的字典中的JSON格式数据。我有另一个名为dic1的NSDicitonary,我... 查看详情

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

】无法创建Plist文件。iOS+NSDictionary+JSON【英文标题】:PlistFilecannotbecreated.iOS+NSDictionary+JSON【发布时间】:2014-12-0804:36:46【问题描述】:我在创建使用JSON保存从Web服务接收的数据的Plist文件时遇到问题。无法创建Plist文件。路径为... 查看详情

如何将 NsDictionary 值从 tableview 传递到下一个视图

】如何将NsDictionary值从tableview传递到下一个视图【英文标题】:HowtopassNsDictionaryvaluetonextviewfromtableview【发布时间】:2011-05-0305:50:49【问题描述】:嗨,我正在从具有NsDictionary的模型类中获取uitableview中的值。这是我的代码-(UITable... 查看详情

IOS/Objective-C:从 NSManagedObject 构建 NSDictionary

】IOS/Objective-C:从NSManagedObject构建NSDictionary【英文标题】:IOS/Objective-C:BuildNSDictionaryfromNSManagedObject【发布时间】:2018-07-0919:45:10【问题描述】:我正在尝试从NSManagedObject构建NSDictionary。我的印象是你可以这样做:NSMutableDictionary*... 查看详情

仅将更新的数据从 NSDictionary 解析为核心数据

】仅将更新的数据从NSDictionary解析为核心数据【英文标题】:ParsingonlyupdateddatafromNSDictionaryintoCoreData【发布时间】:2013-09-2519:28:27【问题描述】:我以NSDictionary的形式接收有关用户的数据,并将其解析为CoreData实体。问题是解析... 查看详情

如何将 NSDictionary 解析为 NSUserDefaults IOS7

】如何将NSDictionary解析为NSUserDefaultsIOS7【英文标题】:howtoparseNSDictionaryintoNSUserDefaultsIOS7【发布时间】:2014-05-2611:25:53【问题描述】:在我的应用程序中,我想在UILabel中显示远程通知。所以我正在尝试将通知消息从“Appdelegate”... 查看详情

从 NSDictionary 中提取数据

】从NSDictionary中提取数据【英文标题】:PullingDatafromanNSDictionary【发布时间】:2015-10-2418:30:55【问题描述】:所以我有一个包含各种数据的NSDictionary。打印到日志时,打印如下:["user_id":3016817,"grade":"A","percent":"93","grading_periods":["a... 查看详情

UITableView 从 NSMutableArray / NSDictionary 循环输出数据

】UITableView从NSMutableArray/NSDictionary循环输出数据【英文标题】:UITableViewLoopingOutDataFromNSMutableArray/NSDictionary【发布时间】:2009-07-2113:34:13【问题描述】:我目前正在构建一个iPhone应用程序,该应用程序将显示来自名为“stories”的N... 查看详情

从 NSDictionary 检索数据

】从NSDictionary检索数据【英文标题】:RetrievingdatafromaNSDictionary【发布时间】:2010-07-0816:25:31【问题描述】:我不太确定我是否了解如何执行以下操作。如果我想添加例如字典的作者和标题(键,值)。用数据填充该字典,然后... 查看详情

从 uitableview 将数据存储在 nsdictionary 中

】从uitableview将数据存储在nsdictionary中【英文标题】:storedatainnsdictionaryfromuitableview【发布时间】:2014-12-1809:53:11【问题描述】:我有一个自定义的可编辑UITableview,我的问题是当我点击提交按钮时如何将它的值存储在NSDictionary中... 查看详情

访问从 NSJSONSerialization 生成的 NSDictionary 中的 JSON 数据

】访问从NSJSONSerialization生成的NSDictionary中的JSON数据【英文标题】:AccessingJSONdatainsideanNSDictionarygeneratedfromNSJSONSerialization【发布时间】:2011-12-2110:20:20【问题描述】:在访问以下URL(http://jamesstenson.com/portraits/?json=1)中的JSON数据时... 查看详情

从 NSDictionary 中删除密钥

】从NSDictionary中删除密钥【英文标题】:DeletingkeyfromNSDictionary【发布时间】:2011-10-2422:12:35【问题描述】:在我的应用程序中,我将一组数据集存储在NSUserDefaults中。当需要访问它时,我创建了一个数组(NSUserDefaults中的每个键... 查看详情

返回的 JSON 数据从 NSDictionary 变为 NSArray

】返回的JSON数据从NSDictionary变为NSArray【英文标题】:ReturnedJSONdataischangingfromNSDictionarytoNSArray【发布时间】:2015-06-1703:04:42【问题描述】:我在使用和迭代JSON服务时遇到了一点困难。当我使用该服务时,由于记录的数量,我正在... 查看详情

无法从 NSDictionary 获取值是 ios

】无法从NSDictionary获取值是ios【英文标题】:UnabletogetvaluesfromNSDictionaryisios【发布时间】:2012-12-2002:59:38【问题描述】:我的NSDictionary输出与NSLog显示:"filename":"mytest100.txt","filedesc":"FileDescriptionhere","data":[]但是当我这样做时:NSStr... 查看详情

从 iOS NSDictionary 的 json 记录中消除最后一个逗号

】从iOSNSDictionary的json记录中消除最后一个逗号【英文标题】:EliminatingthelastcommafromjsonrecordsforiOSNSDictionary【发布时间】:2012-12-2109:55:51【问题描述】:这是我在app.js中的代码app.get(\'/all_question\',function(req,res)varlistQuestion=function(err,... 查看详情

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

】在PushforSegue[IOS]中获取NSDictionary选定行【英文标题】:GetNSDictionarySelectedrowinPushforSegue[IOS]【发布时间】:2012-10-1315:51:17【问题描述】:大家好,我有一组字典plist文件供我阅读。我正在使用PushforSegue方法将我的数据(字典)传... 查看详情