如何从我的NSDirectory中获取数据(而不仅仅是属性错误)? [英] How do I get out data from my NSDirectory (not just a property typo)?

查看:78
本文介绍了如何从我的NSDirectory中获取数据(而不仅仅是属性错误)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想错过一些简单的事情,但是现在已经好几天没有解决这个问题了。甚至开始创建一个解决方案只是为了解决它现在,但仍然想要解决这个正确的方式。有什么建议么?谢谢!

I'm missing something simple I think, but been at it for days now without solving this. Even Started to create a "work-around" just to solve it for now, but still want to solve this the "right" way. Any suggestions? Thank's!

问题:

似乎缺少类Adealer(获取错误 - [Adealer objectAtIndex:]:无法识别的选择器发送到实例0x8c5f8b0),但我将导入的Adealer.h导入此detailsVC。 但不仅仅是将属性命名为错误的简单错误 objectForKey:@CustName而不是custname等 - 对此进行了大量测试)。

Seems to be missing the class Adealer (get error "-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0"), but I did the import Adealer.h to this "detailsVC". But it's not just a simple error of naming the property wrong (objectForKey:@"CustName" instead of "custname" etc - tested this a lot).

此外,我有类似的listVC,没有类似Adealer的类,它们也以相同的方式将数据传输到相同的detailsVC并且它们工作正好!然后我只是通过调用得到数据;

Also, I've got similar "listVC"s without a class like Adealer in them, that also transfer data the same way to the same "detailsVC" and they work just fine! Then I just get the data with calls like;

self.labelRestName.text = [restDetails objectForKey:@"CustName"];

概述:

我有一个tableViewControllerlistVC,它创建数据并显示一个列表,然后是一个ViewControllerdetailsVC来显示详细信息。数据(listVC中的选定行对象通过seque和destVC.restGPSTransfer(NSDictionary)传输。数据在detailsVC中到达,在终端中看起来像这样;

I got a tableViewController "listVC" that creates the data and show a list, then a ViewController "detailsVC" to show the details. The data (selected row object in "listVC" is transfered via a seque and "destVC.restGPSTransfer" (NSDictionary). The data arrives ok in the "detailsVC" and looks like this in the terminal;

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获取数据到NSMutableArrayrestFeed - 确定。

1) First fetching data from web via a AFNetworking json object into an NSMutableArray "restFeed" - ok.

2)然后创建我自己的将此循环中的NSMutableArray数据转换为NSMutableArrayupdatedDealers - ok;

2) Then creating my own data to an NSMutableArray within this loop into a 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中的经销商进行排序upda tedDealers进入距离顺序,最后用这个创建新的NSMutableArrayrestFeed; (listVC中也有 @synthesize dealerFeed = _dealerFeed; ) - 确定。

3) Then I use NSSortdescriptor to sort the dealers in NSMutableArray "updatedDealers" into distance order and finally creates the new NSMutableArray "restFeed" with this; (also did "@synthesize dealerFeed = _dealerFeed;" in "listVC") - ok.

    _dealerFeed = [NSMutableArray arrayWithArray:sortedContestArray];

4)使用此数组填充一些tableViewCells并且它工作正常 - 好吧。

4) The populating some tableViewCells with this array and it works just fine - ok.;

    cell.cellDealerName.text = [NSString stringWithFormat:@"%@",[[_dealerFeed objectAtIndex:indexPath.row]dealerName]];

5)在函数didSelectRowAtIndexPath中使用detailsVC的NSDictionaryrestGPSTransfer传输所选对象 - 好;

5) In the function didSelectRowAtIndexPath transfer the selected object with the "detailsVC"'s NSDictionary "restGPSTransfer" - ok;

    destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];

detailsVC:

6)数据似乎转移好(在这篇文章的顶部),但在尝试使用时调用数据;

6) The data seems to transfer ok (se top of this post) but when trying to call the data with;

    self.labelRestName.text = [restGPSTransfer objectForKey:@"dealerName"];

我收到此错误并且应用程序崩溃: - [Adealer objectAtIndex:]:无法识别的选择器发送到实例0x8c5f8b0。

I get this error and the app crashes: "-[Adealer objectAtIndex:]: unrecognized selector sent to instance 0x8c5f8b0".

进行了更多测试......

试图验证结构+它是NSDictionaryrestGPSTransfer的关键和属性,但到目前为止使用描述只是让我了。并没有解决我的问题,我仍然得到无法识别的选择器错误。难道它可能已成为字典中的字典或其他东西吗?

Tried to verify the structure + it's keys and properties of the NSDictionary "restGPSTransfer", but using description only got me so far. And have not solved my problem and I still get the "unrecognized selector" error. Could it maybe have become dictionaries within dictionary's or something?

构建这个简单的if-test来查看该属性是否真的存在。但我必须手动检查每个房产。有一个更聪明的方法来检查洞NSDictionary / NSArray?

Constructed this little simple if-test to see if the property is really there. But I have to check every property "manually". There's propably a smarter way to check the hole NSDictionary / NSArray?

if ([restGPSTransfer objectForKey:@"dealerName"]) {
    NSLog(@"= YES! key exists.");
} else {
    NSLog(@"= Nope! key don't exists");
}

感谢任何帮助: - )

THANK'S for any help on this :-)

更新Adealer类文件;

Adealer.h

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

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


推荐答案

已解决!
如果其他人需要,请发布。

SOLVED! Posted if anyone else needs it here.

解决方案

在我的detailsVC中,我首先做了这个iVar声明;

In my "detailsVC" I first did this iVar declaration;

.h:

Adealer *theDealer;
@property (nonatomic, retain) Adealer *theDealer;

.m:

@synthesize theDealer;

然后在我的listVC中,我这样做是为了将Adealer对象及其属性传递给detailsVC (请记住,在所描述的循环中,Adealer对象已经拥有了它的属性);

Then in my "listVC" i did this to transfer the Adealer object and it's properties to the "detailsVC" (remember that the Adealer object already has got it's properties earlier in the described "loop");

而不是我之前的;

destVC.restGPSTransfer = [_dealerFeed objectAtIndex:myIndexPath.row];

我将其更改为;

destVC.theDealer = [_dealerFeed objectAtIndex:myIndexPath.row];

要实际显示并检查detailsVC中的转移属性,我现在可以简单地调用此方法来获取经销商名称(或任何其他属性);

And to actually show and check the transferred property in "detailsVC" I can now simply call this to get the dealers name (or any other properties);

self.labelRestName.text = theDealer.dealerName;
NSLog(@"theDealer.name = %@",theDealer.dealerName);

效果很好! Happy Coding everyone!

Works great! Happy Coding everyone!

这篇关于如何从我的NSDirectory中获取数据(而不仅仅是属性错误)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆