- [__ NSArrayM objectForKeyedSubscript:]:无法识别的选择器发送到实例0x7fb30bf13ac0' xcode错误

时间:2015-01-15 16:38:50

标签: ios objective-c unrecognized-selector

我正在尝试将为我创建的同事称为Web服务,但我收到此错误:

-[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fb30bf13ac0' xcode error

我是xcode的新手,还在学习。一个简单的基本解释会对我有很大帮助

下面我还会附上我的m和h文件代码

PlayerProgViewController.m文件

    #import "PlayerProgViewController.h"

//step to make the flickrDictionary into a ivar instead of a local variable of the didReceieveData method
//added a mutable array so we can parse the dictionary and put the objects into the array for use in the tableview
@interface PlayerProgViewController()
{
    NSDictionary * flickrDictionary;
    NSMutableArray * cFRAIPArray;
}

@end


@implementation PlayerProgViewController

@synthesize playerTableView;

//test for an array for the data to take up
@synthesize myData;

//a method that shows the user what is appearing on the screen
-(void)viewDidLoad
{
    [super viewDidLoad];

//to help create the empty array that the data will occupy (TEST)
//TRYING TO FIX ERROR! (COMMENT OUT FOR NOW)
    //self.dataArray = [[NSMutableArray alloc]init];

//TEST to call web service!!!!!
    NSURL * myURL = [NSURL URLWithString:@"http://1.1.1.20:8080/Andrews/players"];
    NSURLRequest * myRequest = [NSURLRequest requestWithURL:myURL];
    NSURLConnection * myConnection = [NSURLConnection connectionWithRequest:myRequest delegate:self];
    }


//TEST to call web service!!!!!...HERE TO..
//this method displays an error code or sends data if an error occurs or not
//if the response recieved is good, the server begins to send data
//if good goes to didRecieveData method
//if an error occurs then goes to didFailWithError method
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//with the webservice availble, goes here from _dataarray
    NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
    int errorCode = httpResponse.statusCode;
    NSString * fileMIMEType = [[httpResponse MIMEType]lowercaseString];
    NSLog(@"response is %d, %@",errorCode, fileMIMEType);
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"data is %@",data);

    //shows the same result in our web browser that we want to call
    //next step to put it in an IOS object so we can pass it around and do whatever we want with it
    NSString * myString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"string is %@",myString);

    //steps to GET the data (putting it in an object)
    //this manipulates the data from the web page to a NSDictionary
    //NSError * e = nil;

    //NSDictionary * ..not needed since we declared the dicitonary on top
    flickrDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

     NSLog(@"dictionary is %@",flickrDictionary);

//dummy code
    /*
    flickrDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e];
    if(![flickrDictionary isKindOfClass:[NSDictionary class]])
    {
        //handle error
    }
    else
    {
        //NSArray * newArray = flickrDictionary[@"TeamRosterProject"];
        //OR
        cFRAIPArray = flickrDictionary[@"TeamRosterProject"];
    }
    */



}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //to inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo]objectForKey:NSURLErrorFailingURLStringErrorKey]);
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //do something with the data
    //receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded!");

    //now we parse the dictionary only once it is fully populated from the web fetch
    //originally 12 was set as 5
    cFRAIPArray = [[NSMutableArray alloc]initWithCapacity:12];
    for (NSString * key in flickrDictionary){
        NSLog(@"the key is %@",flickrDictionary[key]);
//APP CRASHES HERE!!!!!!===============================
        id object = flickrDictionary[key];

        if ([object isKindOfClass:[NSDictionary class]]) {

            //use firstName to replace _content?
            //firstName is the valueForKey?
            NSLog(@"object valueForKey is %@",[object valueForKey:@"_content"]);
            [cFRAIPArray addObject:[object valueForKey:@"_content"]];




        }else{
            [cFRAIPArray addObject:flickrDictionary[key]];
        }
    }

    NSLog(@"cfraiparray %@", cFRAIPArray);
    [self.playerTableView reloadData];
}


//...HERE

//these methods are called every time the view will do this or that
//views appear many times but are only loaded once
-(void)viewWillAppear:(BOOL)animated{
}
-(void)viewDidAppear:(BOOL)animated{
}
-(void)viewWillDisappear:(BOOL)animated{
}

//required methods in a tableview
//all called once,but didSelectRow..gets called many times
/*
numberOfSectionsInTableView
numberOfRowsInSection
cellForRowAtIndexPath
 //set here?
didSelectRowAtIndexPath
*/


#pragma mark - UITableView Data Source
//UITableView Delegate requires the 3 methods below!

//TRYING TO FIX ERROR! (COMMENT OUT FOR NOW)
/*
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;//return the number of sections
    }
 */

//TRYING TO FIX ERROR! (COMMENT OUT FOR NOW)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //??????why as _dataArray and not as dataArray
//PAUSES HERE AND NOTHING IS RETURNED (POINT OF ERROR)
        return [_dataArray count];
        //return the number of rows in the section
        //if you are serving data from an array, return the length of the array
        //categorry is the name of the array
    }



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowIndexPath:(NSIndexPath *)indexPath{
        static NSString *MyIdentifier = @"Identifier";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if(cell==nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
        }


    //OLD WAY TO SET DATA IN THE TABLE
    //[cell.textLabel setText:[dict valueForKey:@"TeamID"]];


//sets data for this cell (needed?)
    /*
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = @"More text";
    cell.imageView.image = [UIImage imageNamed:@"name of image.png"];
     //sets the accessory view
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    */

    return cell;


    //keep this
    cell.textLabel.text = [cFRAIPArray objectAtIndex:indexPath.row];
//used to set data to the detail text label (save for later)
    cell.detailTextLabel.text = [cFRAIPArray objectAtIndex:indexPath.row];

    }



@end

PlayerProgViewController.h文件

    #import <UIKit/UIKit.h>

//(test)
/*
@protocol PlayerModelProtocol <NSObject>
-(void)itemsDownloaded:(NSArray *)items;
@end
*/

@interface PlayerProgViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>


//(test2) continued
/*
@interface PlayerModelProtocol:NSObject
@property (nonatomic, weak) id<PlayerModelProtocol> delegate;
-(void)downloadItems;
*/


//@property (strong,nonatomic)NSArray * myPlayerData;

@property (weak, nonatomic) IBOutlet UITableView *playerTableView;

@property NSMutableArray * dataArray;

//contains all the data i created (TEST)
@property (strong, nonatomic) NSArray * myData;

//SETTING PROPERTIES (testing with firstName)
//@property (nonatomic, strong)NSString * firstName;



@end

这是应用程序中断的地方

NSLog(@"the key is %@",flickrDictionary[key]);

        id object = flickrDictionary[key];

1 个答案:

答案 0 :(得分:1)

你得到一个数组。所以迭代一个数组:

for (NSDictionary * player in flickrArray) // The former flickrDictionary, rename it
{ 
  NSLog(@"the player is %@",player);

  if ([player isKindOfClass:[NSDictionary class]]) 
  {
    NSLog(@"object valueForKey is %@",[player valueForKey:@"_content"])
…
  }
}