到目前为止,我试图将硬编码数据设置为
self.transactions = @[@"Transaction 1", @"Transaction 2", @"Transaction 3"];
一切似乎都很好。现在我更改了此内容以从JSON
文件中获取数据,然后它崩溃了应用程序。我改变的相关部分是
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
NSDictionary *transactionsDictionary = [TransactionAPI getTransactionsForYear:2014 AndMonth:9];
NSLog(@"Transactions:%@", [transactionsDictionary valueForKey:@"transactions"]);
NSLog(@"Transactions Is Array:%@", @([[transactionsDictionary valueForKey:@"transactions"] isKindOfClass:[NSArray class]]));
self.transactions = [transactionsDictionary valueForKey:@"transactions"];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
}
return self;
,代理代码是
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.transactions.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = self.transactions[(NSUInteger) indexPath.row];
return cell;
}
我看到的日志是
2014-09-01 11:43:53.798 pennyapp-ios[51286:70b] Transactions:(
{
amount = "25.0";
category = "Food & Drink";
debit = 1;
name = "White Elephant Thai";
"transaction_date" = "Mon, 01 Sep 2014 03:01:52 -0000";
uuid = "5fe79315-1378-4b3f-90b7-e820d5c786d8";
},
{
amount = "2.0";
category = "Food & Drink";
debit = 1;
name = "Apple Juice";
"transaction_date" = "Fri, 29 Aug 2014 22:02:02 -0000";
uuid = "1c4f3931-2de8-4791-b2b7-34fc1bb113a5";
},
{
amount = "4.99";
category = "Food & Drink";
debit = 1;
name = "India Bazar";
"transaction_date" = "Fri, 29 Aug 2014 21:59:26 -0000";
uuid = "bdcdd72f-f46b-47c1-b6fb-ba877e9ef1ce";
},
{
amount = "48.59";
category = "Food & Drink";
debit = 1;
name = Costco;
"transaction_date" = "Mon, 25 Aug 2014 01:37:32 -0000";
uuid = "153a4ca7-1a6e-471a-80a4-d5d087e18bef";
},
{
amount = "1.0";
category = "Food & Drink";
debit = 1;
name = "Patel Brothers";
"transaction_date" = "Sun, 24 Aug 2014 21:34:11 -0000";
uuid = "8e351586-00eb-41ef-8429-189ef31c3bad";
},
{
amount = "60.81";
category = Utilities;
debit = 1;
name = Comcast;
"transaction_date" = "Sun, 24 Aug 2014 20:23:37 -0000";
uuid = "36f24ec4-b906-425c-8983-b07f10dc2d63";
},
{
amount = "8.82";
category = Personal;
debit = 1;
name = "Samsung Tablet Case";
"transaction_date" = "Sun, 24 Aug 2014 20:17:13 -0000";
uuid = "2ee4a1fe-47b9-4b47-b908-e7eb9647e4e9";
},
{
amount = "320.0";
category = "Gifts & Donations";
debit = 1;
name = "Harit Gift";
"transaction_date" = "Sat, 23 Aug 2014 00:01:27 -0000";
uuid = "e070e9d1-7439-4169-a5d1-71e637723e1f";
},
{
amount = "9.0";
category = "Gifts & Donations";
debit = 1;
name = "Patel Brothers";
"transaction_date" = "Sat, 23 Aug 2014 00:00:02 -0000";
uuid = "8b8af28a-c679-411c-806a-52c35d0267c8";
},
{
amount = "9.63";
category = "Food & Drink";
debit = 1;
name = Sprouts;
"transaction_date" = "Mon, 18 Aug 2014 00:23:10 -0000";
uuid = "ea5624fe-8e76-4133-9f4b-238e9f823474";
},
{
amount = "51.01";
category = "Health & Medical";
debit = 1;
name = Vitamins;
"transaction_date" = "Sun, 17 Aug 2014 01:25:31 -0000";
uuid = "101d16e3-cc95-41e5-ba44-1c9ee0a6a2ce";
},
{
amount = "23.0";
category = "Food & Drink";
debit = 1;
name = Costco;
"transaction_date" = "Sun, 17 Aug 2014 01:23:35 -0000";
uuid = "33b495c1-fe7c-4a70-b8fa-8198c377f00b";
},
{
amount = "11.47";
category = "Health & Medical";
debit = 1;
name = Kaiser;
"transaction_date" = "Thu, 14 Aug 2014 21:47:23 -0000";
uuid = "8863196c-cb10-4f64-ab8c-cbf068ad05df";
},
{
amount = "31.47";
category = Transportation;
debit = 1;
name = Chevron;
"transaction_date" = "Thu, 14 Aug 2014 21:47:04 -0000";
uuid = "8422f5de-5733-4d81-9081-f5c69d8a629a";
},
{
amount = "10.0";
category = Personal;
debit = 1;
name = Laundry;
"transaction_date" = "Tue, 12 Aug 2014 03:20:52 -0000";
uuid = "7773fe4b-ee18-4343-9bb3-0e3072c3c56e";
},
{
amount = "10.5";
category = "Food & Drink";
debit = 1;
name = "India Bazar";
"transaction_date" = "Sun, 10 Aug 2014 23:18:13 -0000";
uuid = "409bd274-5f2d-4ba4-86c6-90f5f5014fe0";
},
{
amount = "8.0";
category = Home;
debit = 1;
name = "Dollar Tree";
"transaction_date" = "Sun, 10 Aug 2014 23:17:49 -0000";
uuid = "eaf23fca-8f5b-4e7a-93dc-1c4e29274351";
},
{
amount = "12.98";
category = "Food & Drink";
debit = 1;
name = "Patel Brothers";
"transaction_date" = "Sun, 10 Aug 2014 16:20:11 -0000";
uuid = "3dfba99b-7278-450e-8e82-ee9761181294";
},
{
amount = "18.47";
category = Personal;
debit = 1;
name = "Payless footwears";
"transaction_date" = "Sun, 10 Aug 2014 01:05:08 -0000";
uuid = "925d9cf1-9fc7-4062-99ad-4fdfb6eaf225";
},
{
amount = "8.28";
category = "Food & Drink";
debit = 1;
name = "India Bazar";
"transaction_date" = "Thu, 07 Aug 2014 04:17:18 -0000";
uuid = "6cab973d-7641-467c-9284-ebd441e97750";
},
{
amount = "17.59";
category = "Health & Medical";
debit = 1;
name = Kaiser;
"transaction_date" = "Tue, 05 Aug 2014 18:25:33 -0000";
uuid = "eb23dc2d-276e-4bc6-8fd5-bd952558465a";
},
{
amount = "28.0";
category = Transportation;
debit = 1;
name = Chevron;
"transaction_date" = "Tue, 05 Aug 2014 04:17:44 -0000";
uuid = "a34e57f3-0877-4763-a7fe-955f00a36ec3";
},
{
amount = "1818.0";
category = Home;
debit = 1;
name = Rent;
"transaction_date" = "Mon, 04 Aug 2014 03:20:19 -0000";
uuid = "df2047a3-112e-4d82-b1ba-c09a66bb69cd";
},
{
amount = "49.5";
category = "Food & Drink";
debit = 1;
name = Costco;
"transaction_date" = "Mon, 04 Aug 2014 03:19:04 -0000";
uuid = "9599590e-d8e1-4cdd-a81f-3d6bbac79f66";
},
{
amount = "64.36";
category = "Food & Drink";
debit = 1;
name = "India Bazar";
"transaction_date" = "Mon, 04 Aug 2014 03:18:54 -0000";
uuid = "44d30643-bba3-471a-a8f8-2f71aa2e5f1d";
},
{
amount = "35.0";
category = Utilities;
debit = 1;
name = "Phone Metro PCS";
"transaction_date" = "Sat, 02 Aug 2014 21:48:45 -0000";
uuid = "2fb93a75-69f9-49cf-a93a-f7343dcba59f";
},
{
amount = "60.81";
category = Utilities;
debit = 1;
name = Comcast;
"transaction_date" = "Sat, 02 Aug 2014 14:36:46 -0000";
uuid = "112c43eb-6e5e-4b4d-9079-6d896fa11b01";
}
)
2014-09-01 11:43:53.801 pennyapp-ios[51286:70b] Transactions Is Array:1
2014-09-01 11:43:53.809 pennyapp-ios[51286:70b] -[__NSCFDictionary length]: unrecognized selector sent to instance 0x10bf65a50
2014-09-01 11:43:53.811 pennyapp-ios[51286:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0x10bf65a50'
*** First throw call stack:
(
0 CoreFoundation 0x00000001023c7495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010212699e objc_exception_throw + 43
2 CoreFoundation 0x000000010245865d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001023b8d8d ___forwarding___ + 973
4 CoreFoundation 0x00000001023b8938 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001009b259d -[UILabel font] + 34
6 UIKit 0x0000000100b320cc -[UITableViewCellLayoutManager layoutSubviewsOfCell:] + 5307
7 UIKit 0x0000000100a593be -[UITableViewCell layoutSubviews] + 71
8 UIKit 0x0000000100891993 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
9 QuartzCore 0x0000000104fbd802 -[CALayer layoutSublayers] + 151
10 QuartzCore 0x0000000104fb2369 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
11 QuartzCore 0x0000000104fb21ea _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
12 QuartzCore 0x0000000104f25fb8 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
13 QuartzCore 0x0000000104f27030 _ZN2CA11Transaction6commitEv + 394
14 QuartzCore 0x0000000104f2769d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
15 CoreFoundation 0x0000000102392dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
16 CoreFoundation 0x0000000102392d37 __CFRunLoopDoObservers + 391
17 CoreFoundation 0x0000000102372522 __CFRunLoopRun + 946
18 CoreFoundation 0x0000000102371d83 CFRunLoopRunSpecific + 467
19 GraphicsServices 0x00000001036e4f04 GSEventRunModal + 161
20 UIKit 0x0000000100831e33 UIApplicationMain + 1010
21 pennyapp-ios 0x00000001000013f3 main + 115
22 libdyld.dylib 0x00000001033665fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
问题是什么?我无法调试这个。感谢
答案 0 :(得分:1)
问题是,您没有维护一组事务,但是您正在维护它的字典。在目标C中类型强制是棘手的,NSDictionary
传递为NSArray
并且您的应用在numberOfItemsInSection
崩溃。
但是,要解决此问题,您需要创建一个类似于JSON结构的模型。并创建这些模型的数组,并将其作为dataSource
。在cellForItemAtIndexPath
,您可以通过
TransactionModel *model = [transactions objectAtIndex:indexPath.row];
cell.label.text = model.name;
//So on and so forth
编辑#1
如果您在创建模型文件时遇到困难,请按照以下说明进行操作:
TransactionModel.h
@interface TransactionModel : NSObject
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *category;
//So on and so forth...
- (id) initWithTransactionDictionary:(NSDictionary*)dictionary;
initWithTransactionDictionary方法
-(id) initWithTransactionDictionary:(NSDictionary*)dictionary {
self = [super init];
if(self) {
if([dictionary objectForKey:@"name"] != nil) {
self.name = [dictionary objectForKey:@"name"];
}
//So on and so forth...
}
return self;
}
编辑#2
如果您遇到创建交易数组的问题,请在initWithStyle
方法
for(int i = 0; i < [transactions count]; i++) {
TransactionModel *currentModel = [[TransactionModel alloc]initWithDictionary:[transactions objectAtIndex:i]];
[self.transactionModels addObject:currentModel];
}
答案 1 :(得分:1)
您曾经在NSStrings
数组中拥有transactions
。并且执行以下操作是正确的:
cell.textLabel.text = self.transactions[(NSUInteger) indexPath.row];
您将获取一个字符串并将其作为文本应用。但现在数组中有字典而不是字符串。但是你仍然试图从数组中取出一个对象并将其作为文本应用。这是不可能的,因为现在有词典。字典没有方法length
。这就是你的应用程序崩溃的原因。你应该从字典中取一个字符串(最有可能是name
字段)并将其作为文本应用