我想念一些简单的东西,但是已经好几天没有解决这个问题了。甚至开始创建一个“解决方法”只是为了暂时解决它,但仍然想以“正确”的方式解决这个问题。有什么建议么?谢谢!
.
问题:
似乎缺少 Adealer 类(获取错误“-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0”),但我将 Adealer.h 导入到这个“detailsVC”。但这不仅仅是将属性命名错误的简单错误(objectForKey:@"CustName"
而不是“custname”等 - 对此进行了很多测试)。
此外,我有类似的“listVC”,其中没有像 Adealer 这样的类,它们也以相同的方式将数据传输到相同的“detailsVC”,它们工作得很好!然后我只需通过以下调用获取数据;
self.labelRestName.text = [restDetails objectForKey:@"CustName"];
概述:
我有一个创建数据并显示列表的 tableViewController“listVC”,然后是一个 ViewController“detailsVC”来显示详细信息。数据(“listVC”中的选定行对象通过序列和“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
.
跟随数据:
“列表VC”
1) 首先通过 AFNetworking json 对象从 web 获取数据到 NSMutableArray “restFeed” - 好的。
2)然后在这个循环中创建我自己的数据到 NSMutableArray 到 NSMutableArray “updatedDealers” - 好的;
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";@synthesize dealerFeed = _dealerFeed;
(在“listVC”中也做了“ ”)-好的。
_dealerFeed = [NSMutableArray arrayWithArray:sortedContestArray];
4)用这个数组填充一些tableViewCells,它工作得很好 - 好的。;
cell.cellDealerName.text = [NSString stringWithFormat:@"%@",[[_dealerFeed objectAtIndex:indexPath.row]dealerName]];
5) 在函数 didSelectRowAtIndexPath 中使用“detailsVC”的 NSDictionary “restGPSTransfer”传输选定的对象 - 好的;
destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];
“详细信息”:
6)数据似乎可以正常传输(本帖顶部),但是在尝试调用数据时;
self.labelRestName.text = [restGPSTransfer objectForKey:@"dealerName"];
我收到此错误并且应用程序崩溃:“-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0”。
又做了一些测试...
试图验证结构 + 它是 NSDictionary“restGPSTransfer”的键和属性,但使用描述只让我到目前为止。并没有解决我的问题,我仍然得到“无法识别的选择器”错误。它可能已经成为字典或其他东西中的字典吗?
构造了这个简单的 if-test 来查看属性是否真的存在。但我必须“手动”检查每个属性。可能有一种更聪明的方法来检查 NSDictionary / NSArray 的漏洞?
if ([restGPSTransfer objectForKey:@"dealerName"]) {
NSLog(@"= YES! key exists.");
} else {
NSLog(@"= Nope! key don't exists");
}
感谢您对此的任何帮助:-)
.
更新 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
经销商.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