How do I get out data from my NSDirectory (not jus

2019-07-27 11:43发布

问题:

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!

.

The problem:

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).

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"];

Overview:

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

.

Following the data:

"listVC"

1) First fetching data from web via a AFNetworking json object into an NSMutableArray "restFeed" - 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) 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) The populating some tableViewCells with this array and it works just fine - ok.;

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

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

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

"detailsVC":

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"];

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

Some more testing done...

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?

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 :-)

.

UPDATE the Adealer class files;

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

回答1:

SOLVED! Posted if anyone else needs it here.

The solution

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

.h:

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

.m:

@synthesize theDealer;

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");

Instead of my earlier;

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

I changed it to;

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

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);

Works great! Happy Coding everyone!