I'm trying to get a JSON feed from Instagram to show data in my UITableView. However I get the following error and I can't figure out what the problem is:
2013-08-18 10:20:37.361 ttoauth[38213:c07] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x75d7480
2013-08-18 10:20:37.362 ttoauth[38213:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x75d7480'
*** First throw call stack:
(0x13c8012 0x11ede7e 0x14534bd 0x13b7bbc 0x13b794e 0x3f68 0x1ec8fb 0x1ec9cf 0x1d51bb 0x1e5b4b 0x1822dd 0x12016b0 0x26abfc0 0x26a033c 0x26a0150 0x261e0bc 0x261f227 0x261f8e2 0x1390afe 0x1390a3d 0x136e7c2 0x136df44 0x136de1b 0x22c57e3 0x22c5668 0x131ffc 0x2a4d 0x2975)
libc++abi.dylib: terminate called throwing an exception
Here is my code:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [jsonResults count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *instafeed_tableview = [jsonResults objectAtIndex:indexPath.row];
NSString *username_label = [[instafeed_tableview objectForKey:#"from"] valueForKey:#"username"];
cell.username.text = [NSString stringWithFormat:#"%#", username_label];
cell.username.clipsToBounds = YES;
cell.contentView.clipsToBounds = NO;
return cell;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(CGFloat)tableView :(UITableView *)tableView heightForRowAtIndexPath :(NSIndexPath *)indexPath {
return 191;
}
What am I doing wrong?
reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x75d7480
This line tells you that you are using objectAtIndex (NSArray method) to NSDictionary. The error couse becouse what you receive is NSDictionary and you want to use it like NSArray.
Probably your error couse line is:
NSDictionary *instafeed_tableview = [jsonResults objectAtIndex:indexPath.row];
Try to debug it. I think your jsonResults is NSDictionary not NSArray.
Add breakpoint to this line and in output type: po [jsonResults class];.
EDIT based on comment
You got this wrong. This not you who setting what data type do you want from JSON. JSON structure determines that. For example:
[
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
This example retturns NSArray contains NSDictionaries in it.
{
"name":"John" ,
"surname":"Doe"
},
This will return single NSDictionary
This [] and this {} brackets determines what object are you dealing with.
Related
i am receiving a chat message from signalR server. now i want to display the chat message in tableview..so i am writing like this.. my response is like this “” "(\n\"(alex" Hi)”)..”alex” is user name and “hi” is text message.i want to display these two items in tableview. but my response is having special characters.so i am writing my code like this.
-(void)messagesReceived:(id)data
{
if(data)
{
NSArray *arrCount;
NSArray *arr=(NSArray *)data;
arrCount=[NSArray arrayWithObjects:arr, nil];
NSString *str1=[arrCount description];
NSArray* strArray = [str1 componentsSeparatedByString: #","];
NSString* userName = [strArray objectAtIndex:0];
NSLog(#"user Name is :%#",userName);
NSArray *arrName=[userName componentsSeparatedByString:#""];
NSLog(#"arr is:%#",arrName);
NSString *messageText=[strArray objectAtIndex:1];
NSLog(#"message array is:%#",messageText);
messageText=[messageText stringByReplacingOccurrencesOfString:#")\"\n)" withString:#""];
messageText=[messageText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *arr1=[messageText componentsSeparatedByString:#""];
NSLog(#"array value is:%#",arr1);
itemsDataArr=[NSMutableArray arrayWithObjects:arr1, nil];
NSLog(#"Items array is:%#",itemsDataArr);
[_ChattblView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [itemsDataArr count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [_ChattblView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if (itemsDataArr.count>0) {
cell.textLabel.text=[itemsDataArr objectAtIndex:indexPath.row];
}
return cell;
}
but i got exception this line of code..
cell.textLabel.text=[itemsDataArr objectAtIndex:indexPath.row];
the exception is like this
-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7c15b730”
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7c15b730'
NSMutableDictionary *persondict=[[NSMutableDictionary alloc]init];
[persondict setObject:fullName forKey:#"givenName"];
[persondict setObject:identity forKey:#"identifier"];
[persondict setObject:_mobnumberarray forKey:#"CNLabelPhoneNumberMobile"];
[persondict setObject:_iphoneArray forKey:#"CNLabelPhoneNumberiPhone"];
[_Contacts addObject:persondict];
Here above is my dictionary populated with two arrays, but when I try to display the _Contacts array on tableview it gives me following error on the console screen:
[__NSArrayM length]: unrecognized selector sent to instance
0x7fd4d15211d0
Here is my table view method:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return _Contacts.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"tableViewCell";
tableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.celll.text = [[_Contacts objectAtIndex:indexPath.row]valueForKey:#"givenName"];
cell.idcell.text=[[_Contacts objectAtIndex:indexPath.row]valueForKey:#"identifier"];
cell.numbercell.text=[[_Contacts objectAtIndex:indexPath.row]valueForKey:#"CNLabelPhoneNumberMobile"];
cell.iphonecell.text=[[_Contacts objectAtIndex:indexPath.row]valueForKey:#"CNLabelPhoneNumberiPhone"];
return cell;
}
Any help will be appreciated. Thanks in advance.
If _Contact is of type NSMutableArray then try this, else replace the type:
_Contact = [[NSMutableArray alloc] init];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return dict.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"RestaurantResultsCell";
RestaurantDetailsViewCell *cell = (RestaurantDetailsViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"restaurantDetailsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
arrResponse =[NSArray arrayWithObject:dict];
arr = [arrResponse objectAtIndex:0];
dictio = [arr objectAtIndex:0];
cell.cusineRestLabel.text = [[dictio valueForKey:#"restaurant_name"]objectAtIndex:indexPath.row];
NSLog(#"cell is %#",cell.cusineRestLabel.text);
// cell.cusineRestLabel.text = [dic objectForKey:#"restaurant_name"];
cell.cusineRestAddress.text = [dictio valueForKey:#"restaurant_streetaddress"];
NSLog(#"cell is %#",cell.cusineRestAddress.text);
cell.cusineDeliveryTime.text = [dictio valueForKey:#"restaurant_delivery"];
NSLog(#"cell is %#",cell.cusineDeliveryTime.text);
return cell;
}
after executing for the first time [__NSArrayI length]: unrecognized selector sent to instance
This code
arrResponse =[NSArray arrayWithObject:dict];
arr = [arrResponse objectAtIndex:0];
is a very strange way to write
arr = (id)dict;
which looks not like a bug, but intentional obfuscation. So what is it: An array or a dictionary?
I have a problem with my code and i can't figure out what's wrong.
My code is this :
#implementation FirstViewController {
NSDictionary *countriesDetails;
NSArray *countriesList;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"countries" withExtension:#"plist"];
countriesDetails = [NSDictionary dictionaryWithContentsOfURL:url];
countriesList = countriesDetails.allKeys;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return countriesDetails.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.text = [[countriesList objectAtIndex:indexPath.row] objectForKey:#"country"];
return cell;
}
My plist file is this :
<dict>
<key>Paris</key>
<dict>
<key>country</key>
<string>France</string>
<key>chName</key>
<string>Paris</string>
<key>chlink</key>
<string>www.paris.com</string>
</dict>
<key>Rome</key>
<dict>
<key>country</key>
<string>Italy</string>
<key>chName</key>
<string>Rome</string>
<key>chlink</key>
<string>www.rome.com</string>
</dict>
So my app will have multiple tableviews, and the first one shows all the countries I have on my plist.
My XCode shows no error, but when I am running the app it crashes and this appears:
2015-05-26 23:34:53.362 newApp[1511:95592] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fde31f242d0
2015-05-26 23:34:53.366 neaApp[1511:95592] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fde31f242d0'
* First throw call stack:
Your countriesList consists of NSStrings, not NSDictionarys.
Change your tableView:cellForRowAtIndexPath: method to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.text = [[countriesDetails objectForKey:[countriesList objectAtIndex:indexPath.row]] objectForKey:#"country"];
return cell;
}
I’ve got this error when populating a tableview from an nsmutablearray.
The arrays are added to the dictionary and the dictionary added to the mutable array, but the app crashes each time the view is loaded with error:
[__NSArrayI isEqualToString:]: unrecognized selector sent to instance
0x8a87e60 2014-05-24 12:23:02.589 jwStudy[77652:907] * Terminating
app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI isEqualToString:]: unrecognized selector sent to
instance 0x8a87e60'
The log output confirms that the arrays and keys are added correctly.
But why does the cell in the cellForRowAtIndexPath: method throw an error?
Here’s the code:
- (void)viewDidLoad
{
[super viewDidLoad];
languageArray = [[NSMutableArray alloc] init];
languages = [[NSArray alloc] initWithObjects:#"Abbey", #"Abkhasisk", #"Abua", nil];
downloadURL = [[NSArray alloc] initWithObjects:#"http://download.jw.org/files/content_assets/bb/502013341_ABB_cnt_1_r480P.mp4",
#"http://download.jw.org/files/content_assets/3c/502013341_ABK_cnt_1_r480P.mp4",
#"http://download.jw.org/files/content_assets/7e/502013341_AU_cnt_1_r720P.mp4", nil];
mutedict = [NSDictionary dictionaryWithObjectsAndKeys:languages, #"FirstKey", downloadURL, #"SecondKey", nil];
[languageArray addObject:mutedict];
NSLog(#"%#",languageArray);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return languageArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// ERROR HERE!!!
cell.textLabel.text = [[languageArray objectAtIndex:indexPath.row] objectForKey:#"FirstKey”];
if ([checkCellLanguage isEqual:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#Farhan and Joy.
Thank you both for prompt reply. I have realized now that I wasn't clear enough in my question. What I wanted was for each selected row in the tableview to register the row's corresponding URL. So I ended up abandoning the arrays and dictionaries ... too confusing. And going with 2 NSArrays instead. This link got me going on the right track and everything works now:
http://sweettutos.com/2012/08/25/loading-urls-from-uitableview/
In your code, two arrays stored in a dictionary , and the dictionary stored in a array.
Like this :
languageArray ----- mutedict ------ languages (FirstKey)
|
|
----------- downloadURL(SecondKey)
cell.textLabel.text is a NSString, but [[languageArray objectAtIndex:indexPath.row] objectForKey:#"FirstKey”]; is NSArray
So your code throw an error.
If your want put a language as cell.textLabel.text
You could modify your code as cell.textLabel.text = [[mutedict objectForKey:#"FirstKey"] objectAtIndex:indexPath.row]
Hope it can help you :)