Items in NSDictionary returns NULL - iphone

I'm using MGTWitterEngine and I cannot figure out why my dictionary items are returning null.
I have this method:
- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier{
NSDictionary *result = [searchResults objectAtIndex:0];
NSString *fromUser = [result valueForKey:#"from_user"];
NSLog(#"from user: %#", fromUser);
}
And for some reason, my NSLog always displays "from user: NULL". I can do an NSLog of searchResults which dumps the contents of the search correctly, but I can't figure out how to parse the information. Any help would be greatly appreciated.

Look at this question: Parsing Search Result with MGTwitterEngine in Objective C
They use:
- (void)searchResultsReceived:(NSArray *)searchResults
forRequest:(NSString *)connectionIdentifier
{
if ([searchResults count] > 0)
{
NSDictionary *result = [searchResults objectAtIndex:0];
NSString *fromUser = [result valueForKey:#"from_user"];
NSString *fromUserID = [result valueForKey#"from_user_id"];
// ...
NSString *text = [result valueForKey#"text"];
NSLog(#"User %#(%#): %#", fromUser, fromUserID, text);
}
}
It is similar to your code with a check on searchResults count.

Related

NSArray componentsSeperatedByString Sigabrt

I get a Sigabrt at the NSlog and i have no idea why - any suggestions?
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [contentList componentsSeparatedByString:#"$$"];
NSLog(#"%#%#",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
Following Joes suggestions, I now got:
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [[contentList componentsSeparatedByString:#"$$"] retain];
if ([contentArray count] > 0) {
NSLog(#"%#%#",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
}
Which gives me an EXC_BAD_ACCESS at the NSLog thing.
I get a Sigabrt at the NSlog
Your NSLog statement is trying to print an integer as if it was an object:
NSLog(#"%#%#",contentList,[contentArray count]);
^
Here!
Replace %# with %d.
You can read more on format specifiers in the String Programming Guide.
You are not checking to make sure you have at least 1 element in your array. Accessing [contentArray objectAtIndex:0] will be an issue if the contentArray is empty.

iOS XML Parsed Data Comparison

I am parsing data using the NSXMLParser delegate. I know the data is being parsed because I can call upon it in the NSLog. I am having trouble running a conditional statement that compares whether a string equals a certain value.
Here is my code:
NSString *status = [NSString stringWithFormat:#"%#", [attributeDict objectForKey:#"status"]];
NSLog(#"Status: %#", status);
if (status == #"1") {
NSLog(#"Test succeeded!");
}
The NSString 'status' will read '1' in the NSLog, but the if statement above will not be called. Is this a casting problem?
Thank you in advance.
Cheers, Evan.
use isEqualToString function of NSString.
So your code would be like below.
if ([status isEqualToString:#"1"])
{
NSLog(#"Test succeeded!");
}
Read NSString documentation
NSString *status = [NSString stringWithFormat:#"%#", [attributeDict objectForKey:#"status"]];
NSLog(#"Status: %#", status);
if ([status isEqualToString:#"1"] == TRUE) {
NSLog(#"Test succeeded!");
}
More information about NSString

iPhone: can i use NSString search in NSArray

can i use like this r not
for (NSString *string in userInfo){
lblUserName.text = (NSString *) [userInfo objectForKey:#"name"];
lblLocation.text = (NSString *) [userInfo objectForKey:#"location"];
lblDescription.text =(NSString *) [userInfo objectForKey:#"description"];
NSLog(#"User profileData Received: %#", userInfo);
}
here userInfo = NSArray (this is delegate i can change )
lblUserName = label name
ObectForKey is using for searching NSString
thank you, is this Right r wrong one
if not how i have to work out
NSArray are not key based but index based. Use NSDictionary instead or use index to retrieve items.
and I don't understand what is the loop for, you don't even use the string var in it ???

TableView UISearchBar on Tab Bar Controller Crashes while Searching

I've been playing around with a search facility for my application table view for a while now trying to get it working but i keep getting the same error in my console.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' [NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance
I believe that this following section may be the problem I have tried passing some NSLog entries inside the if statement and it seems to get through it but the problem is when I click on the search bar and starting typing, the first letter I type calls the error and cancels my app.
Here is where the problem is
In View Will Appear "Food" Array is initialized as below:
NSString *myDBnew =#"/Users/taxsmart/Documents/rw3app.sql";
database = [[Sqlite alloc] init];
[database open:myDBnew];
NSString *quer = [NSString stringWithFormat:#"Select category from foodcat"];
Food = [database executeQuery:quer];
//[database executeNonQuery:quer];
[database close];
Search bar delegate method where error is encountered:
(void) searchTableView
{
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
// [searchArray addObjectsFromArray:Food];
for(NSDictionary *dictionary in Food)
{
NSString temp1 = [dictionary objectForKey:#"category"];
[searchArray addObject:temp1];
}
for (NSString *sTemp in searchArray)
{
NSLog(#"Value: %#",NSStringFromClass([sTemp class]));
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
[searchArray release];
searchArray = nil;
}
What should I do?
Please Help.
Please Suggest
Thanks
It looks that result of database query (Food) is dictionary that contains dictionary. This code:
for(NSDictionary *dictionary in Food)
{
NSString temp1 = [dictionary objectForKey:#"category"];
[searchArray addObject:temp1];
}
can be replaced with:
for(NSDictionary *dictionary in Food)
{
NSObject *ob = [dictionary objectForKey:#"category"];
if([ob isKindOfClass: [NSString class]])
{
[searchArray addObject:ob];
}
else if([ob isKindOfClass: [NSDictionary class]])
{
NSDictonary *dic1 = (NSDictionary*)ob;
// ... at this point you can get the string for desired dictionary key
// or you can ignore it
}
}
With this code we can be sure that only strings are put into searchArray.
If you want to make full tree parsing for desired key 'category' then you should make some recursive function to search the dictionary.
You can dump Food variable to console to see at which leaf is actually the result you are looking for. Put the break-point and into console type 'po Food'.
Appears that there is an NSDictionary in your dataArray.
Add an
NSLog(#"%#",NSStringFromClass([description class]]));
To see which classes your dataArray contains.

Help With UISearchBar Methods

So I'm having trouble implementing a search bar in my app.
The methods find the filtered items but for some reason they won't show up in my tableview.
I think it has something to do with adding the objects to the filteredListContentArray.
What object should I be adding for this to work.
Here's my code:
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (NSDictionary *dictionary in tableDataSource)
{
NSString *testString = [dictionary valueForKey:#"Title"];
NSLog(#"String list to be Searched is %#", testString);
//NSLog(#"Contents of list are %#", testString);
NSComparisonResult result = [testString compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
//NSObject *filteredObject = [dictionary objectForKey:#"Title"];
if (result == NSOrderedSame)
{
NSLog(#":-)");
NSLog(#"Resulted object is %#", [dictionary valueForKey:#"Title"]);
[self.filteredListContent addObject:dictionary];
}
else
{
NSLog(#":-(");
}
}
NSLog(#"Contents of Filtered list are %#", self.filteredListContent);}
That last NSLog reads (null) every time, but the NSLog Above it always shows the correct filtered items.
where do you allocate memory for your filteredListContent? and there is tableDataSource array. do you fill your table from filteredListContent or from tableFataSource array? also you can try to print to console [filteredListContent description];