我正在尝试将从JSON Dictionary收到的数组显示到UITableView上但我一直收到错误。我试图将给定Instagram图片的所有评论显示在表格上。以下是我尝试显示单元格的方法:
MessageTableViewCell *cell = (MessageTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:MessengerCellIdentifier];
NSDictionary *instaPics = self.instagramData;
NSArray *commentArray = instaPics[@"comments"][@"data"];
NSString *commentText = [commentArray valueForKey:@"text"]; // Error occurs here
Message *message = self.messages[indexPath.row];
cell.titleLabel.text = message.username;
cell.bodyLabel.text = commentText;
以下是我收到的错误:
-[__NSArrayI length]: unrecognized selector sent to instance 0x7f913052db10
2015-05-05 22:16:08.848 Floadt[6061:514032] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7f913052db10'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107d60c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010763cbb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000107d680ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000107cbe13c ___forwarding___ + 988
4 CoreFoundation 0x0000000107cbdcd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000106543ab4 -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 65
6 UIKit 0x000000010654390f -[UILabel textRectForBounds:limitedToNumberOfLines:] + 76
7 UIKit 0x00000001065473b9 -[UILabel _intrinsicSizeWithinSize:] + 170
8 UIKit 0x0000000106547499 -[UILabel intrinsicContentSize] + 76
9 UIKit 0x00000001069e5b6c -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 33
10 UIKit 0x00000001069e5930 -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 422
11 UIKit 0x00000001069ecd25 -[UIView(AdditionalLayoutSupport) updateConstraints] + 162
12 UIKit 0x0000000106547304 -[UILabel updateConstraints] + 274
13 UIKit 0x00000001069ec346 -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 242
14 UIKit 0x00000001069ec53e -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124
15 CoreFoundation 0x0000000107c67354 CFArrayApplyFunction + 68
16 UIKit 0x00000001069ec2ed -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 153
17 Foundation 0x0000000105b021be -[NSISEngine withBehaviors:performModifications:] + 155
18 UIKit 0x00000001069ec53e -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124
19 CoreFoundation 0x0000000107c67354 CFArrayApplyFunction + 68
20 UIKit 0x00000001069ec2ed -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 153
21 UIKit 0x00000001069ec53e -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124
22 UIKit 0x00000001069eca0e __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke + 96
23 UIKit 0x00000001069ec6d6 -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 231
24 UIKit 0x00000001067d7531 -[UITableViewCellContentView updateConstraintsIfNeeded] + 94
25 UIKit 0x00000001069ecdde -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 146
26 UIKit 0x00000001063e4a3d -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 114
27 UIKit 0x00000001063f0a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
28 QuartzCore 0x00000001061b4ec2 -[CALayer layoutSublayers] + 146
29 QuartzCore 0x00000001061a96d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
30 UIKit 0x00000001063e4675 -[UIView(Hierarchy) layoutBelowIfNeeded] + 607
31 Floadt 0x0000000104195848 -[SLKTextViewController viewWillAppear:] + 296
32 UIKit 0x00000001064a4fa1 -[UIViewController _setViewAppearState:isAnimating:] + 487
33 UIKit 0x00000001064c6395 -[UINavigationController _startCustomTransition:] + 887
34 UIKit 0x00000001064d23bf -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
35 UIKit 0x00000001064d2f0e -[UINavigationController __viewWillLayoutSubviews] + 43
36 UIKit 0x000000010661d715 -[UILayoutContainerView layoutSubviews] + 202
37 UIKit 0x00000001063f0a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
38 QuartzCore 0x00000001061b4ec2 -[CALayer layoutSublayers] + 146
39 QuartzCore 0x00000001061a96d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
40 QuartzCore 0x00000001061a9546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
41 QuartzCore 0x0000000106115886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
42 QuartzCore 0x0000000106116a3a _ZN2CA11Transaction6commitEv + 462
43 UIKit 0x000000010636d626 _UIApplicationHandleEventQueue + 2140
44 CoreFoundation 0x0000000107c94431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
45 CoreFoundation 0x0000000107c8a2fd __CFRunLoopDoSources0 + 269
46 CoreFoundation 0x0000000107c89934 __CFRunLoopRun + 868
47 CoreFoundation 0x0000000107c89366 CFRunLoopRunSpecific + 470
48 GraphicsServices 0x0000000109716a3e GSEventRunModal + 161
49 UIKit 0x0000000106370900 UIApplicationMain + 1282
50 Floadt 0x0000000104218c5f main + 111
51 libdyld.dylib 0x0000000109f2b145 start + 1
52 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
以下是JSON的示例副本:
{
attribution = "<null>";
caption = {
"created_time" = 1430877943;
from = {
"full_name" = "Shreya M.";
id = 25085898;
"profile_picture" = "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/10890672_320752588135679_1815913692_a.jpg";
username = "shreya_mandava";
};
id = 978597576956579585;
text = "9+10????";
};
comments = {
count = 2;
data = (
{
"created_time" = 1430878007;
from = {
"full_name" = "Aleesha Toteja";
id = 17117902;
"profile_picture" = "https://instagramimages-a.akamaihd.net/profiles/profile_17117902_75sq_1373927314.jpg";
username = leeshtotes;
};
id = 978598112887967529;
text = "DIRT \Ud83d\Ude3b";
},
{
"created_time" = 1430878307;
from = {
"full_name" = "Shritha M.";
id = 375799796;
"profile_picture" = "https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xpf1/t51.2885-19/10454159_1056687271012469_2048676049_a.jpg";
username = "shritha_m";
};
id = 978600630846098439;
text = sigh;
}
);
};
"created_time" = 1430877943;
filter = Normal;
id = "978597574314167442_25085898";
images = {
"low_resolution" = {
height = 306;
url = "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s306x306/e15/11241842_1401517966839213_670743424_n.jpg";
width = 306;
};
"standard_resolution" = {
height = 640;
url = "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/11241842_1401517966839213_670743424_n.jpg";
width = 640;
};
thumbnail = {
height = 150;
url = "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e15/11241842_1401517966839213_670743424_n.jpg";
width = 150;
};
};
likes = {
count = 22;
data = (
{
"full_name" = "Ronak Chopra";
id = 347652824;
"profile_picture" = "https://scontent.cdninstagram.com/hphotos-xpa1/t51.2885-19/11142328_1644920965736964_1040275960_a.jpg";
username = "conak_rhopra";
},
{
"full_name" = "emily bach";
id = 292615252;
"profile_picture" = "https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xap1/t51.2885-19/10454121_1558984144374824_122699173_a.jpg";
username = "emily_bach_";
},
{
"full_name" = "\Ud83c\Udf38Trisha\Ud83c\Udf38";
id = 303629882;
"profile_picture" = "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-19/10955125_708435799269374_749376734_a.jpg";
username = trishddishh;
},
{
"full_name" = "Shritha M.";
id = 375799796;
"profile_picture" = "https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xpf1/t51.2885-19/10454159_1056687271012469_2048676049_a.jpg";
username = "shritha_m";
}
);
};
link = "https://instagram.com/p/2UrU8bDgyS/";
location = {
id = 772010926;
latitude = "38.913782928";
longitude = "-77.383238738";
name = "ft. dirt";
};
tags = (
);
type = image;
user = {
"full_name" = "Shreya M.";
id = 25085898;
"profile_picture" = "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/10890672_320752588135679_1815913692_a.jpg";
username = "shreya_mandava";
};
"user_has_liked" = 0;
"users_in_photo" = (
{
position = {
x = "0.6703125";
y = "0.246875";
};
user = {
"full_name" = "John Nguyen";
id = 1477626916;
"profile_picture" = "https://igcdn-photos-d-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-19/11018467_1431648953794187_659343544_a.jpg";
username = bumbonguyen;
};
}
);
}
答案 0 :(得分:0)
您正试图从NSArray中的密钥获取值:
[commentArray valueForKey:@"text"];
NSArray不是字典没有键(有多个注释),应该通过他的索引访问。
[[commentArray objectAtIndex:0] valueForKey:@"text"];
可能你会想要,迭代评论和每个评论得到他的“文本”
答案 1 :(得分:0)
您可以替换此行:
NSString *commentText = [commentArray valueForKey:@"text"];
带
for (NSDictionary *comment in commentArray ) {
NSString *commentText = [comment valueForKey:@"text"];
cell.bodyLabel.text = commentText;
}
贡萨洛是对的。您访问的commentArray就像NSDictionary,但它实际上是一个NSArray。这导致了崩溃。
答案 2 :(得分:0)
NSArray * commentArray = instaPics [@&#34;评论&#34;] [@&#34;数据&#34;]; 它看起来像关键值的错误访问.. 根据我的理解,你正试图获得评论的价值。
你的Json数据包含root&amp;子键值,如
评论
计数
数据
此处您的评论具有子数值,如计数,数据。 所以可以编写代码来获取评论值&gt;数据&gt;文字
-------- -------- 1
NSMutableDictionary *response = [[[json valueForKey:@"comments"] objectAtIndex:0]mutableCopy];
NSMutableDictionary *data = [[[response valueForKey:@"data"] objectAtIndex:0]mutableCopy];
NSString *text = [data valueForKey:@"text"];
---- ---- OR 2 --------
NSMutableDictionary *response = [[[json valueForKey:@"comments"] objectAtIndex:0]mutableCopy];
NSArray *textarr=[response valueForKey:@"text"];