Having trouble comparing two strings from Twitter API on iPhone - iphone

I'm trying to get tweets using the Twitter API, but for some reason this problem is coming up whenever I try to refresh the tweets:
[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6c22e70
Getting tweets for the first time and filling "tweetssofar" is happening properly, and when I NSLog fromtwitternew and existingtwitter, both are showing the same thing. Both are strings that are actually dictionaries, but why cannot I compare them this way? Thanks!
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier {
NSString *fromtwitternew = [statuses objectAtIndex:0];
NSString *existingtwitter = [tweetssofar objectAtIndex:0];
NSLog(#"No problem so far");
if ([fromtwitternew isEqualToString:existingtwitter]) { //No new tweets
NSLog(#"No new tweets");
contents = [[NSDictionary alloc] init];
contents = [tweetssofar objectAtIndex:counter];
}

isEqualToString: only works for strings, and those tweets are probably json or xml strings parsed into dictionaries by a parser. If you want to know whether two parsed tweets are equal, you can compare them like this:
[[tweetA valueForKey:#"id"] isEqualToNumber:[tweetB valueForKey:#"id"]];

#Prajoth with the following code , I am sure you will be able to fix your problem
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier {
if ([statuses count] > 0 && [tweetssofar count] > 0) {
id fromtwitternew = [statuses objectAtIndex:0];
if ([fromtwitternew isKindOfClass:[NSDictionary class]]) {
//Handle code accroding to Dictionary Results
contents = [[NSDictionary alloc] init];
contents = [tweetssofar objectAtIndex:counter];
}
else if ([fromtwitternew isKindOfClass:[NSString class]]) {
//Make Sure that you have added Only Strings to tweetssofar , in case You have added dictionary you can not use isEqualToString
NSString *existingtwitter = [tweetssofar objectAtIndex:0];
if ([fromtwitternew isEqualToString:existingtwitter]) { //No new tweets
NSLog(#"No new tweets");
}
}
}
}
The Crash is most probably because you are comparing isEqualToString: Method to compare two dictionary objects which you should not be doing. In case you need to compare two tweets you should first extract the Strings from dictionary and then compare them.

Related

How to get the objects in an array based on particular string

I am new to iphone.I have an array which contains the objects like below
"04_Num",
"04_Num/04Num.m3u",
"04_Num/04Num001.mp3",
"04_Num/04Num002.mp3",
"04_Num/04Num003.mp3",
"04_Num/04Num004.mp3",
"04_Num/04Num005.mp3",
"04_Num/04Num006.mp3",
"04_Num/04Num007.mp3",
"04_Num/04Num008.mp3",
"04_Num/04Num009.mp3",
"04_Num/04Num010.mp3",
"04_Num/04Num011.mp3",
"04_Num/04Num012.mp3",
"04_Num/04Num013.mp3",
"04_Num/04Num014.mp3",
"04_Num/04Num015.mp3",
"04_Num/04Num016.mp3",
"04_Num/04Num017.mp3",
"04_Num/04Num018.mp3",
"04_Num/04Num019.mp3",
"04_Num/04Num020.mp3",
"04_Num/04Num021.mp3",
"04_Num/04Num022.mp3",
"04_Num/04Num023.mp3",
"04_Num/04Num024.mp3",
"04_Num/04Num025.mp3",
"04_Num/04Num026.mp3",
"04_Num/04Num027.mp3",
"04_Num/04Num028.mp3",
"04_Num/04Num029.mp3",
"04_Num/04Num030.mp3",
"04_Num/04Num031.mp3",
"04_Num/04Num032.mp3",
"04_Num/04Num033.mp3",
"04_Num/04Num034.mp3",
"04_Num/04Num035.mp3",
"04_Num/04Num036.mp3"
but here i want the objects only which contains .mp3 extension and then i have to place those objects into another array
how it is possible if any body know this please help me...
You can iterate and get only the one that has .mp3
Like so
//yourArrayThatContainsAllStrings contains all the strings
NSMutableArray *arrayOfMp3 = [[NSMutableArray alloc] init];
for (NSString *str in yourArrayThatContainsAllStrings) {
if ([str rangeOfString:#".mp3"].location != NSNotFound) {
[arrayOfMp3 addObject:str];
}
}
//arrayOfMp3 will contain only the .mp3 files
NSMutableArray *mpthrees = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *file in songs) //Where songs is the array with the paths you have provided
{
BOOL isMpthree = [[file pathExtension] isEqualToString:#"mp3"];
if (isMpthree) [mpthrees addObject:file];
}
// Now mpthrees array holds only paths pointing to .mp3 files
// Let's call your array of strings as stringsArray
NSMutableArray *filteredArray = [[NSMutableArray alloc] init];
for (NSString *str in stringsArray) {
if ([str hasSuffix:#".mp3"]) {
[filteredArray addObject:str];
} }
//filteredArray will contain only the strings ending with ".mp3"
P.S. Since you're just beginning Objective C, i would like to reiterate that Objective C NSString objects always start with an # symbol. #"04_Num/04Num001.mp3"

How do I find (not remove) duplicates in an NSDictionary of NSArrays?

The title pretty much says it all, but just to clarify: I have an NSMutableDictonary containing several NSMutableArrays. What I would like to do is find any value that is present in multiple arrays (there will not be any duplicates in a single array) and return that value. Can someone please help? Thanks in advance!
Edit: For clarity's sake I will specify some of my variables:
linesMutableDictionary contains a list of Line objects (which are a custom NSObject subclass of mine)
pointsArray is an array inside each Line object and contains the values I am trying to search through.
Basically I am trying to find out which lines share common points (the purpose of my app is geometry based)
- (NSValue*)checkForDupes:(NSMutableDictionary*)dict {
NSMutableArray *derp = [NSMutableArray array];
for (NSString *key in [dict allKeys]) {
Line *temp = (Line*)[dict objectForKey:key];
for (NSValue *val in [temp pointsArray]) {
if ([derp containsObject:val])
return val;
}
[derp addObjectsFromArray:[temp pointsArray]];
}
return nil;
}
this should work
If by duplicates you mean returning YES to isEqual: you could first make an NSSet of all the elements (NSSet cannot, by definition, have duplicates):
NSMutableSet* allElements = [[NSMutableSet alloc] init];
for (NSArray* array in [dictionary allValues]) {
[allElements addObjectsFromArray:array];
}
Now you loop through the elements and check if they are in multiple arrays
NSMutableSet* allDuplicateElements = [[NSMutableSet alloc] init];
for (NSObject* element in allElements) {
NSUInteger count = 0;
for (NSArray* array in [dictionary allValues]) {
if ([array containsObject:element]) count++;
if (count > 1) {
[allDuplicateElements addObject:element];
break;
}
}
}
Then you have your duplicate elements and don't forget to release allElements and allDuplicateElements.

Writing an NSMutableArray to my documents directory fails

I am attempting to cache a web request. Basically I have an app that uses a facebook user's friend list but I don't want to grab it every single time they log in. Maybe refresh once per month. Caching the friend list in a plist in the documents directory seems to make sense for this functionality. I do this as follows:
- (void)writeToDisk {
NSLog(#"writing cache to disk, where cache = %#", cache);
BOOL res = [cache writeToFile:[FriendCache persistentPath] atomically:YES];
NSLog(#"reading cache from disk immediately after writing, res = %d", res);
NSMutableArray *temp = [[NSMutableArray alloc] initWithContentsOfFile:[FriendCache persistentPath]];
NSLog(#"cache read in = %#", temp);
}
+ (NSString *)persistentPath {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:#"FriendCache.plist"];
}
These are members of a FriendCache singleton I am using which basically wraps an NSMutableArray. I have verified that the peristentPath method is returning a valid path. As you you can see in the writeToDisk method, I verify there is data in the cache and then I print the result of the write and check if any data could be read back in. There is never data read back in, because the result of the file write is 0.
The output of the cache print is very long, but here is the abbreviated version:
2010-12-28 13:35:23.006 AppName[51607:207] writing cache to disk, where cache = (
{
birthday = "<null>";
name = "Some Name1";
pic = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs1324.snc4/7846385648654.jpg";
"pic_big" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs442.snc4/784365789465746.jpg";
"pic_square" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs1324.snc4/7846357896547.jpg";
sex = female;
status = "<null>";
uid = 892374897165;
},
{
birthday = "<null>";
name = "Some Name2";
pic = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs625.ash1/54636536547_s.jpg";
"pic_big" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs170.ash2/65465656365666_n.jpg";
"pic_square" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs625.ash1/654635656547_q.jpg";
sex = female;
status = "<null>";
uid = 7658436;
},
...
One thing I checked out is when using writeToFile, I must make sure the object I am writing has valid plist objects. I did check this and here is how I construct the cache object:
- (void)request:(FBRequest*)request didLoad:(id)result{
NSMutableArray *friendsInfo = [[[NSMutableArray alloc] init] autorelease];
for (NSDictionary *info in result) {
NSString *friend_id = [NSString stringWithString:[[info objectForKey:#"uid"] stringValue]];
NSString *friend_name = nil;
NSString *friend_sex = nil;
NSString *friend_relationship_status = nil;
NSString *friend_current_location = nil;
if ([info objectForKey:#"name"] != [NSNull null]) {
friend_name = [NSString stringWithString:[info objectForKey:#"name"]];
}
if ([info objectForKey:#"relationship_status"] != [NSNull null]) {
friend_relationship_status = [NSString stringWithString:[info objectForKey:#"relationship_status"]];
}
if ([info objectForKey:#"sex"] != [NSNull null]) {
friend_sex = [NSString stringWithString:[info objectForKey:#"sex"]];
}
if ([info objectForKey:#"current_location"] != [NSNull null]) {
friend_current_location = [[info objectForKey:#"current_location"] objectForKey:#"name"];
}
NSString *friend_pic_square = [info objectForKey:#"pic_square"];
NSString *friend_status = [info objectForKey:#"status"];
NSString *friend_pic = [info objectForKey:#"pic"];
NSString *friend_pic_big = [info objectForKey:#"pic_big"];
NSString *friend_birthday = [info objectForKey:#"birthday"];
NSDictionary *friend_info = [NSDictionary dictionaryWithObjectsAndKeys:
friend_id,#"uid",
friend_name, #"name",
friend_pic_square, #"pic_square",
friend_status, #"status",
friend_sex, #"sex",
friend_pic, #"pic",
friend_pic_big, #"pic_big",
friend_birthday, #"birthday",
friend_relationship_status, #"relationship_status",
friend_current_location, #"current_location",
nil];
// If the friend qualifies as a single of your gender, add to the friend cache
if ( [AppHelpers friendQualifies:friend_info] == YES) {
[[FriendCache sharedInstance] push:friend_info];
}
}
[[FriendCache sharedInstance] writeToDisk];
}
My push method just wraps the NSMutableArray push:
- (void)push:(id)o {
[cache addObject:o];
}
Can you think of any reason why the write would fail?
Thanks!
So as we already pointed out, it's because of the usage of the NSNull objects.
The best way to avoid this is to create an object Friend, with all of the needed properties. Then you can easily set nil values, something not possible with NSDictionary objects (well, you'd have to remove the key, which is not very good practice).
Then, by implementing the NSCoding protocol, you can easily archive (serialize) your custom object.
This is a much better way of handling your data, and it will become MUCH easier in the future. You'll be able to call messages on the Friend objects, something not possible with NSDictionary.
Use NSError-aware API for NSPropertyListSerialization to get the data and the NSData NSError aware write API so you get a meaningful error helping you understand what your problem might be.

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.

read a Dictionary type PSMultiValueSpecifier from NSUserDefault into a UITableView

Does anyone know how to easily read a Dictionary type PSMultiValueSpecifier from NSUserDefault into a UITableView control and save changes back to NSUserDefaults using iPhone SDK 3.0? If so, can you post some specific code to efficiently do this?
First to better access the specifier values inside the bundle I used the following code to turn them into a dictionary.
NSString* settingsBundle = [[[NSBundle mainBundle]
pathForResource:#"Settings"
ofType:#"bundle"]
stringByAppendingPathComponent:#"Root.plist"];
NSDictionary* rootPlist = [NSDictionary dictionaryWithContentsOfFile:settingsBundle];
if (rootPlist == nil)
return nil;
NSArray* specifiers = [rootPlist objectForKey:#"PreferenceSpecifiers"];
NSDictionary *multiValueSpecifier = nil;
for (NSDictionary *specifier in specifiers)
{
if ([[specifier objectForKey:#"Key"] isEqualToString:speficierKey] == YES &&
[[specifier objectForKey:#"Type"] isEqualToString:#"PSMultiValueSpecifier"] == YES)
{
multiValueSpecifier = specifier;
break;
}
}
if (multiValueSpecifier == nil)
return nil;
NSArray* titlesArray = [multiValueSpecifier objectForKey:#"Titles"];
NSArray* valuesArray = [multiValueSpecifier objectForKey:#"Values"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:titlesArray
forKeys:valuesArray];
return dictionary;
Well now if from your UI you did select something you need to store the KEY and not the VALUE of the dictionary.
[[NSUserDefaults standardUserDefaults] setObject:dictionaryKey forKey:#"myMultiValueSpecifier_preference"];