Unknown recieiver 'indexPath'l did you mean NSIndexPath? - objective-c

I'm parsing something right now and i want to display it in a cell i', using this code for my tableview but i also want it without Tablview
NSString *const JsonDataUrl = [NSString stringWithFormat: #"%#", appdownloadurl];
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:JsonDataUrl]
encoding:NSUTF8StringEncoding
error:nil];
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
parser = nil;
[self setTableData:[results objectForKey:#"resultCount"]];
This works great but when i place this line in viewdidload
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
NSLog(#"Apple: %#", [item objectForKey:#"price"]);
And error shows up on this line;
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
//Unknown recieiver 'indexPath'l did you mean NSIndexPath?
does anyone know how to fix this?

Do you declare any variable called indexPath in your viewDidLoad method? You can't just use the indexPath from your table view methods in viewDidLoad arbitrarily.

NSDictionary *item = [tableData objectAtIndex:[indexPath row]]; suggests you are trying to load from an NSArray, you probably want to try something like:
NSDictionary *item = [tableData objectForKey:#"Some Key"];

Related

Make lazy loading faster and responsive in iOS App

In my iOS app I've a class that performs a web request to get an array of informations located in mySQL DB. In this class I've a method that do this taking as input a mySQL query:
- (NSMutableArray *) myreq:(NSString *)query{
// Create NSData object
NSData *dataQuery = [query
dataUsingEncoding:NSUTF8StringEncoding];
// Get NSString from NSData object in Base64
NSString *base64EncodedQuery = [dataQuery base64EncodedStringWithOptions:0];
// Print the Base64 encoded string
NSLog(#"Encoded: %#", base64EncodedQuery);
NSMutableString *strURL = [NSMutableString stringWithFormat:#"http://…=%#“,base64EncodedQuery];
[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:dataURL
options:kNilOptions
error:&error];
NSMutableArray *results = [[NSMutableArray alloc] init];
int numRow = 0;
for (NSArray *arrow in json) {
[results addObjectsFromArray:arrow];
numRow++;
}
return results;
}
This method send a query to a php script that perform immediately this query to MySQL DB and get a json with results. I translate the json in this method and finally return an array with results.
I call myreq in a method
- (void)downloadScope{
_arrID = [[NSMutableArray alloc] init];
_arrIDUsers = [[NSMutableArray alloc] init];
_arrUsernames = [[NSMutableArray alloc] init];
_arrPictures = [[NSMutableArray alloc] init];
[myQueue addOperation:[NSBlockOperation blockOperationWithBlock: ^{
query = #"SELECT ID FROM mytable”;
[_arrID addObjectsFromArray:[self myreq:query]];
for (int i = 0; i < [_arrID count]; i++) {
NSArray *tempArray = [[NSArray alloc] initWithArray:[self myreq:[NSString stringWithFormat:#"SELECT IDUsr,usrn, pictureaddress FROM mytable WHERE ID = %#",_arrID[i]]]];
[_arrIDUsers insertObject:tempArray[0] atIndex:i];
[_arrUsernames insertObject:tempArray[2] atIndex:i];
[_arrPictures insertObject:tempArray[2] atIndex:i];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.tableView reloadData];
}];
}
}]];
[myQueue setSuspended:NO];
}
In tableView I create cells in this way (using SDWebImage):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Identificatore di cella
NSString *identifier = #“cellmy”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.backgroundColor = nil;
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSString *username = [self.arrUsernames objectAtIndex:indexPath.row];
UILabel *cellLabelUsername = (UILabel *)[cell viewWithTag:2];
cellLabelUsername.text = [username uppercaseString];
UIImageView *cellImageProfileSnap = (UIImageView *)[cell viewWithTag:5];
[cellImageProfileSnap sd_setImageWithURL:[NSURL URLWithString:[_arrPictures objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:#“…”]];
}
In viewDidLoad I initialize my NSOperationQueue (defined in my interface):
myQueue = [[NSOperationQueue alloc] init];
[myQueue setMaxConcurrentOperationCount:100];
[myQueue setName:#"com.sada"];
My goal is to make everything faster because the loading in tableView is slow and I think that is not dependent on SDWebImage. Please help me

NSArray BAD ACCESS

I'm trying to do a pickerView but I'm getting bad acess:
here is my code
-(void) viewWillAppear:(BOOL)animated {
list = [[NSArray alloc]init];
[self populateList]
}
-(void) populateList {
NSString *path = [[NSBundle mainBundle] pathForResource:#"nameoffile" ofType:#"txt"];
NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
list = [file componentsSeparatedByString:#"\n"];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return (NSString *)[list objectAtIndex:row]; //here I'm getting bad acces
}
The error is: "Thread 1: EXC_BAD_ACCESS(code=1, address=0xa001cc65)"
NSArray returned by componentsSeparatedByString: is autoreleased value so you need to retain it.
You should remove:
list = [[NSArray alloc]init];
and add retain to:
list = [[file componentsSeparatedByString:#"\n"] retain];

NSString object not initialized properly

Can somebody tell me what I am doing wrong here. I am trying to initialize a string from a list of predefined names, but for some reason it doesn't work for NSString that is a class field:
MyController.h:
#interface MyController:UIViewController
{
NSString *text;
}
MyController.m:
-(void) viewDidLoad
{
[super viewDidLoad];
NSArray *list = [NSArray arrayWithObjects:#"A", #"B", #"C", nil];
NSString* temp= [list objectAtIndex:0]; // temp = "A"
text = [list objectAtIndex:0]; // Why text = nil ????
text = #"Hello"; // does not work either
}
Turns out the debugger is wrong in this case.
Try setting text equal to NSString *text = [NSString stringWithFormat:#"%#", [list objectAtIndex:0]];

How do I NSLog a string from an NSDictionary object?

Working with Json, how can I "NSlog" only the title in this code:
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
// Set text on textLabel
[[cell textLabel] setText:[item objectForKey:#"title"]];
// Set text on detailTextLabel
[[cell detailTextLabel] setText:[item objectForKey:#"description"]];
Something like NSlog(#"%#", title); ?
Thanks!
If I have understood what you are asking correctly, then this should do it:
NSLog(#"%#", [item objectForKey:#"title"]);
If that gives you a compiler warning because the object may not be an NSString, then you can do:
NSString *title = [item objectForKey:#"title"];
NSLog(#"%#", title);
If I've misunderstood your question, please let me know and I will correct my answer accordingly.

UITableView Cell Dynamic Array not Loading

Please help me figure out why the first line does not print the cell text while the second one does:
cell.textLabel.text = [NSString stringWithFormat:#"Hello %#", [swaItems objectAtIndex:indexPath.row]];
cell.textLabel.text = #"Hello";
No error is generated at any time.
Where you want the cell to initialize :
NSArray *swaItems = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
NSString *string = [swaItems objectAtIndex:indexPath];
NSLog(#"%#", string);
cell.textLabel.text = string;
Works for me.