Populate Plist to UITableview - ios

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

Related

uitableviewcell exception error: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7c15b730'

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'

why do i get an error -[__NSArrayM length]: unrecognized selector sent to instance 0x7fd4d15211d0 while assigning data to custom tableView

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

How to Solve Datatype issue in ios

I have one collection view with textbox in each cell.I am fetching values from a nsmutable dictionary and set text box text in the cell using following codes.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
NSDictionary *datadictionary = [_currencyDictionary objectForKey:_currencyNameDetails[_flag]];
cell.currenyRate.text = [datadictionary objectForKey:_currencyRateValueDetails[row]];
return cell;
}
In this situation my application is crashed show error ....
Terminating app due to uncaught exception '
NSInvalidArgumentException', reason: '-[__NSCFNumber length]:
unrecognized selector sent to instance.
I find the reason for that one using below mentioned method.[datadictionary objectForKey:_currencyRateValueDetails[row]] object is is in nsnumber format .
if([[datadictionary objectForKey:_currencyRateValueDetails[row]] isKindOfClass:[NSNumber class]])
{
NSLog(#"nsnumber......");
}
else{NSLog(#"nsstring......");
}
so rewrite my code to
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
NSDictionary *datadictionary = [_currencyDictionary objectForKey:_currencyNameDetails[_flag]];
NSNumber *Value =[[NSNumber alloc]init];
Value = [currenymap objectForKey:_currencyRateValueDetails[row]];
NSString *myString = [Value stringValue];
cell.currenyRate.text = myString;
return cell;
}
This time application crash again show the error
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString stringValue]: unrecognized selector
sent to instance.
How can I solve this issue?
You can use
NSString *myStringWithNumbers = [NSString stringWithFormat:#"%d",[currenymap objectForKey:_currencyRateValueDetails[row]]];
Hope this helps.

Populating a TableView in Objective C

Hey guys I am having an issue passing a JSON return type through an NSArray
.h file:
#property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;
#property (strong, nonatomic) NSArray *finishedGooglePlacesArray;
.m file:
Here is the returned section:
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:returnData //1
options:kNilOptions
error:&error];
self.googlePlacesArrayFromAFNetworking = [json objectForKey:#"original_request"];
[self.tableView reloadData];
Now from there it gets sent to here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:#"name"];
if([tempDictionary objectForKey:#"location"] != NULL)
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Rating: %# of 5",[tempDictionary objectForKey:#"name"]];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Not Rated"];
}
return cell;
}
But it breaks on
NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];
with an error message:
2014-03-08 13:53:26.772 Ripple[45593:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b72810
2014-03-08 13:53:26.776 Ripple[45593:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b72810'
I am not sure what is going on, suggestions or thoughts?
edit:
- (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 [self.googlePlacesArrayFromAFNetworking count];
}
JSON
responseString: {"status":"ok","code":0,"message":"Testing this awesome application out!","from":"Admin Team","original_request":{"name":"david","location":"awesome","trait":"funny"}}
Update:
Screen shot of error:
According to JSON log, [json objectForKey:#"original_request"] returns anNSDictionary`. So First you need to change this line :
#property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;
to
#property (strong, nonatomic) NSDictionary *googlePlacesArrayFromAFNetworking;
It's normal that you get an error after that because NSDictionary doesn't have objectAtIndex selector. You can enumerate through keys and values in NSDictionary.
Than, change your cellForRowAtIndexPath method to :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.googlePlacesArrayFromAFNetworking objectForKey:#"name"];
if([self.googlePlacesArrayFromAFNetworking objectForKey:#"location"] != NULL)
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Rating: %# of 5",[self.googlePlacesArrayFromAFNetworking objectForKey:#"name"]];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"Not Rated"];
}
return cell;
}
It's look like you don't understand what you get from your json...
so, try this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//get the correct type
NSDictionary *tempDictionary = (NSDictionary *)self.googlePlacesArrayFromAFNetworking;
NSString *key = [[tempDictionary allKeys]objectAtIndex:indexPath.row];
cell.textLabel.text = key;
cell.detailTextLabel.text = [tempDictionary objectForKey:key];
return cell;
}

JSON feed in UITableView error

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.

Resources