简体   繁体   English

属性和错误的问题

[英]Problems with properties and ivars

I'm trying to load in some JSON from a webpage, and it works, but then I have some trouble with the auto synthesized properties and stuff. 我正在尝试从网页中加载一些JSON,并且它可以正常工作,但是我在使用自动合成的属性和内容时遇到了一些麻烦。

Here's my .h code: 这是我的.h代码:

@interface MasterViewController : UITableViewController

@property (nonatomic, strong) NSArray *jsonList;
@property (nonatomic, strong) NSDictionary *jsonResults;

here is my .m code: 这是我的.m代码:

NSData *jsonText = [[NSData alloc] initWithBase64EncodedString: [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil] options:0];

self.jsonResults = [NSJSONSerialization JSONObjectWithData:jsonText options:NSJSONReadingMutableContainers error:&error];  
//jsonResults contains 71 records.
self.jsonList = self.jsonResults.allKeys; //errors out here. [__NSArrayM allKeys]: unrecognized selector sent

JSON Sample: JSON示例:

[{"CustomerID":419,"CustomerName":"Alford","Locked":0,"ExpenseReport":1},{"CustomerID":600,"CustomerName":"Allan","Locked":0,"ExpenseReport":0},{"CustomerID":18424,"CustomerName":"Park","Locked":0,"ExpenseReport":0}] [{“ CustomerID”:419,“ CustomerName”:“ Alford”,“ Locked”:0,“ ExpenseReport”:1},{“ CustomerID”:600,“ CustomerName”:“ Allan”,“ Locked”:0, “ ExpenseReport”:0},{“ CustomerID”:18424,“ CustomerName”:“ Park”,“ Locked”:0,“ ExpenseReport”:0}]]

Here's the stack trace: 这是堆栈跟踪:

2017-04-04 11:08:04.798180 GWM[30923:6253087] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-04-04 11:08:04.888706 GWM[30923:6253087] [] ____nwlog_simulate_crash_inner_block_invoke dlopen CrashReporterSupport failed
2017-04-04 11:08:04.889985 GWM[30923:6253087] [] __nwlog_err_simulate_crash simulate crash failed "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-04-04 11:08:04.901282 GWM[30923:6253087] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x000000010f2a4666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x0000000110414006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x00000001103f1555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x00000001103f0572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x00000001103ef298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x000000011040aae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x000000011040a510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x00000001104221f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x000000010f021978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x000000010f04b0cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x000000010f028e17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x000000010f029b4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x000000010f02c385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x000000010f02c059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x000000010f4d74de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x000000010f4d5341 start_wqthread + 13

2017-04-04 14:25:58.281111 GWM[30923:6338888] [] nw_socket_write_close shutdown(12, SHUT_WR): [57] Socket is not connected
2017-04-04 14:25:58.281978 GWM[30923:6338888] [] nw_endpoint_flow_service_writes [1.1 ready socket-flow (satisfied)] Write request has 0 frame count, 0 byte count
2017-04-04 14:25:58.282905 GWM[30923:6338894] [] __tcp_connection_write_eof_block_invoke Write close callback received error: [89] Operation canceled
2017-04-04 14:25:58.378 GWM[30923:6252853] -[__NSArrayM allKeys]: unrecognized selector sent to instance 0x600000247fe0
2017-04-04 14:25:58.396 GWM[30923:6338892] XPC connection interrupted
2017-04-04 14:25:59.741 GWM[30923:6252853] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM allKeys]: unrecognized selector sent to instance 0x600000247fe0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d1bcd4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010c7fd21e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010d22cf04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010d142005 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010d141b88 _CF_forwarding_prep_0 + 120
    5   GWM                                 0x000000010c11a29e -[MasterViewController viewDidLoad] + 1070
    6   UIKit                               0x000000010d782a3d -[UIViewController loadViewIfRequired] + 1258
    7   UIKit                               0x000000010d782e70 -[UIViewController view] + 27
    8   UIKit                               0x000000010d7d11fa -[UINavigationController preferredContentSize] + 198
    9   UIKit                               0x000000010dea4b3d -[UIViewController(UIPopoverController_Internal) _resolvedPreferredContentSize] + 62
    10  UIKit                               0x000000010deace79 -[UIPopoverController _transitionFromViewController:toViewController:animated:] + 146
    11  UIKit                               0x000000010dea53a8 -[UIPopoverController _initWithContentViewController:popoverControllerStyle:] + 887
    12  UIKit                               0x000000010dc160bc -[UISplitViewControllerClassicImpl _setupHiddenPopoverControllerWithViewController:] + 110
    13  UIKit                               0x000000010dc14b68 -[UISplitViewControllerClassicImpl _addOrRemovePopoverPresentationGestureRecognizer] + 121
    14  UIKit                               0x000000010dc1442e -[UISplitViewControllerClassicImpl loadView] + 349
    15  UIKit                               0x000000010d78261c -[UIViewController loadViewIfRequired] + 201
    16  UIKit                               0x000000010d782e70 -[UIViewController view] + 27
    17  UIKit                               0x000000010d64c4b5 -[UIWindow addRootViewControllerViewIfPossible] + 71
    18  UIKit                               0x000000010d64cc06 -[UIWindow _setHidden:forced:] + 293
    19  UIKit                               0x000000010d660519 -[UIWindow makeKeyAndVisible] + 42
    20  UIKit                               0x000000010d5d8f8d -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818
    21  UIKit                               0x000000010d5df0ed -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731
    22  UIKit                               0x000000010d5dc26d -[UIApplication workspaceDidEndTransaction:] + 188
    23  FrontBoardServices                  0x00000001106d96cb __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    24  FrontBoardServices                  0x00000001106d9544 -[FBSSerialQueue _performNext] + 189
    25  FrontBoardServices                  0x00000001106d98cd -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    26  CoreFoundation                      0x000000010d161761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    27  CoreFoundation                      0x000000010d14698c __CFRunLoopDoSources0 + 556
    28  CoreFoundation                      0x000000010d145e76 __CFRunLoopRun + 918
    29  CoreFoundation                      0x000000010d145884 CFRunLoopRunSpecific + 420
    30  UIKit                               0x000000010d5daaea -[UIApplication _run] + 434
    31  UIKit                               0x000000010d5e0c68 UIApplicationMain + 159
    32  GWM                                 0x000000010c123e9f main + 111
    33  libdyld.dylib                       0x000000010f09768d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Ultimately I want to display this with a table view, but I haven't found a way to bind a NSDictionary with a table without putting it into a NSArray first... 最终,我想以表格视图显示此内容,但是我还没有找到一种将NSDictionary与表格绑定的方法,而无需先将其放入NSArray中。

Your JSON is an array of dictionary. 您的JSON是一个字典数组。 But your code assumes the JSON is a dictionary. 但是您的代码假定JSON是字典。

I would get rid of the needless jsonResults property if all you need is the jsonList array. 如果您需要的只是jsonList数组,那么我将摆脱不必要的jsonResults属性。

Then your code should be: 然后您的代码应为:

NSData *jsonText = [[NSData alloc] initWithBase64EncodedString: [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil] options:0];

self.jsonList = [NSJSONSerialization JSONObjectWithData:jsonText options:0 error:&error];
if (self.jsonList == nil) {
    NSLog(@"Error reading JSON: %@", error);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM