ppp ppp - 6 months ago 94
iOS Question

Why its Come [__NSCFString objectAtIndex:]: unrecognized selector sent to instance?

I am Creating UITableview and add Two UITableviewCell Custom Cells (ProductCell and DescripotionCell).I am displaying Data into UILabel. But I am Getting This Error ([__NSCFString objectAtIndex:]: unrecognized selector sent to instance )and UITableviewCell not displaying Data.
Please check my Code.

- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;


[self.detailedTableview registerNib:[UINib nibWithNibName:@"ProductCell" bundle:nil] forCellReuseIdentifier:@"ProductCell"];

[self.detailedTableview registerNib:[UINib nibWithNibName:@"DescriptionCell" bundle:nil] forCellReuseIdentifier:@"DescriptionCell"];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.detailedTableview])
{
if (indexPath.section == 0)
{
return 396;
}
if (indexPath.section == 1)
{
// return UITableViewAutomaticDimension;
return 100;

}
}
else
{
if (indexPath.section == 0)
{
return 396;
}
if (indexPath.section == 1)
{
// return UITableViewAutomaticDimension;
return 100;
}
}
return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifierproduct = @"ProductCell";
static NSString *cellIdentifierDescription = @"DescriptionCell";

if (indexPath.section == 0) {
ProductCell *cellProduct =[tableView dequeueReusableCellWithIdentifier:cellIdentifierproduct];



return cellProduct;
}
else if (indexPath.section ==1)
{
DescriptionCell *cellDes =[tableView dequeueReusableCellWithIdentifier:cellIdentifierDescription];

NSString*urlString = [NSString stringWithFormat:@"http://54.254.171.25/android/bnm/api/servicesAPI.php?request=getProductDetails&pid=1"];
NSURL *url=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *dataDictionary=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *descArray =[dataDictionary objectForKey:@"pd_description"];
NSLog(@"%@",descArray);
cellDes.descriptionLbl.text = [descArray objectAtIndex:indexPath.row];

return cellDes;
}

return nil;
}


Please check

Answer

pd_description is a string. Remember that NSDictionary works with generic objects, or id. So the compiler does not see a problem when you are assigning an NSString into an NSArray. But on runtime, you are trying to call the methods of an Array from a String object, these methods do not exist in the NSString definition resulting in a Unrecognized selector sent to instance.

pd_description: "Linen Blend Material.
 Regular Collar.
 Full Sleeves.
 Slim Fit.
 Solid Pattern.
 Peach Color",

A way to fix this would be to get the string and separate its components.

NSString *pdDescription = [dataDictionary objectForKey:@"pd_description"];
NSMutableArray *descArray = [pdDescription componentsSeparatedByString:@"."];
NSLog(@"%@",descArray);
         cellDes.descriptionLbl.text = [descArray objectAtIndex:indexPath.row];
Comments