我想错了一些简单的东西,但是现在已经好几天没有解决这个问题了。甚至开始创建一个“解决方案”只是为了解决它现在,但仍然想要解决这个“正确”的方式。有什么建议?感谢的!
问题:
似乎缺少类Adealer(获取错误“ - [Adealer objectAtIndex:]:无法识别的选择器发送到实例0x8c5f8b0”),但我将导入的Adealer.h导入此“detailsVC”。 但这不仅仅是将属性命名错误的简单错误(objectForKey:@"CustName"
而不是“custname”等 - 经过多次测试)。
另外,我有类似的“listVC”没有像Adealer这样的类,它们也以相同的方式将数据传输到相同的“detailsVC”,它们工作得很好!然后我只是通过调用获取数据;
self.labelRestName.text = [restDetails objectForKey:@"CustName"];
概述:
我有一个tableViewController“listVC”创建数据并显示一个列表,然后是一个ViewController“detailsVC”来显示详细信息。数据(“listVC”中的选定行对象通过seque和“destVC.restGPSTransfer”(NSDictionary)传输。数据在“detailsVC”中到达,在终端中看起来像这样;
dealerName = Uppsala Centrum Test
dealerAdressStreet = Dragarbrunnsgatan 55
dealerAdressZip = 75320
dealerAdressCity = Uppsala
dealerLongitude = 17.63893
dealerLatitude = 59.85856
dealerDistance2 = 8586398.000000
etc
关注数据:
“listVC”
1)首先通过AFNetworking json对象从Web获取数据到NSMutableArray“restFeed” - 确定。
2)然后在此循环中创建我自己的数据到NSMutableArray到NSMutableArray“updatedDealers” - ok;
NSMutableArray *updatedDealers = [[NSMutableArray alloc]init];
while (i+1 < [_restFeed count]) {
i++;
// Get dealer position function here
// Get distance function here
// Then create my own data here (also #imported Adealer to "listVC";
Adealer *theDealer = [[Adealer alloc]init];
theDealer.dealerName = [[_restFeed objectAtIndex:i]objectForKey:@"CustName"];
theDealer.dealerLongitude = [[_restFeed objectAtIndex:i]objectForKey:@"long"];
theDealer.dealerLatitude = [[_restFeed objectAtIndex:i]objectForKey:@"lat"];
theDealer.dealerDistance2 = theDistance;
// etc...
// Check if data ok
NSLog(@"theDealer = %@",[theDealer description]);
// Don't add dealer object without positiondata to the new array
if (![theDealer.dealerLatitude isEqualToString:@""]) {
[updatedDealers addObject:theDealer];
}
3)然后我使用NSSortdescriptor将NSMutableArray“updatedDealers”中的经销商按距离顺序排序,最后用此创建新的NSMutableArray“restFeed”; (也在“listVC”中做了“@synthesize dealerFeed = _dealerFeed;
”) - 确定。
_dealerFeed = [NSMutableArray arrayWithArray:sortedContestArray];
4)用这个数组填充一些tableViewCells并且它工作正常 - 好吧。
cell.cellDealerName.text = [NSString stringWithFormat:@"%@",[[_dealerFeed objectAtIndex:indexPath.row]dealerName]];
5)在函数didSelectRowAtIndexPath中使用“detailsVC”的NSDictionary“restGPSTransfer”传输所选对象 - ok;
destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];
“detailsVC”:
6)数据似乎转移好了(这篇文章的顶部),但是当试图用数据调用时;
self.labelRestName.text = [restGPSTransfer objectForKey:@"dealerName"];
我收到此错误并且应用程序崩溃:“ - [Adealer objectAtIndex:]:无法识别的选择器发送到实例0x8c5f8b0”。
进行了更多测试......
试图验证NSDictionary“restGPSTransfer”的结构+它的键和属性,但是到目前为止使用描述只能得到我。并没有解决我的问题,我仍然得到“无法识别的选择器”错误。它可能已成为词典中的词典或其他东西吗?
构建这个简单的if-test,看看该属性是否真的存在。但我必须“手动”检查每个属性。有一个更聪明的方法来检查洞NSDictionary / NSArray?
if ([restGPSTransfer objectForKey:@"dealerName"]) {
NSLog(@"= YES! key exists.");
} else {
NSLog(@"= Nope! key don't exists");
}
感谢任何帮助: - )
更新Adealer类文件;
Adealer.h
#import <Foundation/Foundation.h>
@interface Adealer : NSObject
@property (nonatomic, retain) NSString * dealerName;
@property (nonatomic, retain) NSString * dealerAdressCity;
@property (nonatomic, retain) NSString * dealerAdressStreet;
@property (nonatomic, retain) NSString * dealerAdressZip;
@property (nonatomic, retain) NSNumber * dealerID;
@property (nonatomic, retain) NSString * dealerImages;
@property (nonatomic, retain) NSString * dealerLogo;
@property (nonatomic, retain) NSString * dealerMail;
@property (nonatomic, retain) NSString * dealerProducts;
@property (nonatomic, retain) NSString * dealerTel;
@property (nonatomic, retain) NSString * dealerText;
@property (nonatomic, retain) NSString * dealerWeb;
@property (nonatomic, retain) NSString * dealerLongitude;
@property (nonatomic, retain) NSString * dealerLatitude;
@property (nonatomic, retain) NSString *dealerDistance;
@property float dealerDistance2;
@end
Adealer.m
#import "Adealer.h"
@implementation Adealer
@synthesize dealerAdressCity, dealerAdressStreet, dealerAdressZip, dealerID, dealerImages, dealerLogo;
@synthesize dealerMail, dealerName, dealerProducts, dealerTel, dealerText, dealerWeb;
@synthesize dealerLongitude, dealerLatitude, dealerDistance,dealerDistance2;
- (NSString *)description {
// Added extension of description
NSMutableString *string = [NSMutableString string];
[string appendString:@"\ntheDealer object and it's properties:\n"];
[string appendFormat:@"dealerName = %@\n", dealerName];
[string appendFormat:@"dealerAdressStreet = %@\n", dealerAdressStreet];
[string appendFormat:@"dealerAdressZip = %@\n", dealerAdressZip];
[string appendFormat:@"dealerAdressCity = %@\n", dealerAdressCity];
[string appendFormat:@"dealerTel = %@\n", dealerTel];
[string appendFormat:@"dealerMail = %@\n", dealerMail];
[string appendFormat:@"dealerWeb = %@\n", dealerWeb];
[string appendFormat:@"dealerLogo = %@\n", dealerLogo];
[string appendFormat:@"dealerImages = %@\n", dealerImages];
[string appendFormat:@"dealerText = %@\n", dealerText];
[string appendFormat:@"dealerProducts = %@\n", dealerProducts];
[string appendFormat:@"dealerLongitude = %@\n", dealerLongitude];
[string appendFormat:@"dealerLatitude = %@\n", dealerLatitude];
[string appendFormat:@"dealerDistance = %@\n", dealerDistance];
[string appendFormat:@"dealerDistance2 = %f\n\n", dealerDistance2];
return string;
}
@end
答案 0 :(得分:0)
<强>解决!强> 如果其他人需要它,请发布。
解决方案
在我的“detailsVC”中,我首先做了这个iVar声明;
·H:
Adealer *theDealer;
@property (nonatomic, retain) Adealer *theDealer;
的.m:
@synthesize theDealer;
然后在我的“listVC”中,我这样做是为了将Adealer对象及其属性传递给“detailsVC”(请记住,Adealer对象早先已经在所描述的“循环”中获得了它的属性);
而不是我之前的;
destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];
我把它改成了;
destVC.theDealer = [_dealerFeed objectAtIndex:myIndexPath.row];
要实际显示并检查“detailsVC”中的转移财产,我现在可以简单地调用它来获取经销商名称(或任何其他属性);
self.labelRestName.text = theDealer.dealerName;
NSLog(@"theDealer.name = %@",theDealer.dealerName);
效果很好!快乐的编码每个人!