So I have the following code, that splits up the street,town,state,country,zip and makes an AddressAnnotation, a class I have extended from annotation.
Sometimes all of the pins show up on the map and sometimes it cannot find some of the places, although it is a collection of the exact same addresses every time. Is there any reason why Google maps can find a place sometimes but not all the time?
Is it something in my code?
Thanks,
R
for (int i=0; i < [places count]-1; ++i) {
NSArray *arrayPlace = [[places objectAtIndex:i] componentsSeparatedByString:#"***"];
AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:
[self getLocationFromAddressString:
[NSString stringWithFormat:
#"%# %# %# %# %# %#",
[arrayPlace objectAtIndex:3],
[arrayPlace objectAtIndex:4],
[arrayPlace objectAtIndex:5],
[arrayPlace objectAtIndex:6],
[arrayPlace objectAtIndex:7],
[arrayPlace objectAtIndex:8]]]];
[addAnnotation setWebsite:[arrayPlace objectAtIndex:2]];
[addAnnotation setTitle:[arrayPlace objectAtIndex:0]];
[addAnnotation setSubtitle:[arrayPlace objectAtIndex:1]];
[mapView addAnnotation:addAnnotation];
}
Try logging the address strings you are generating. If they are always the same then look into getLocationFromAddressString and see if the coordinates it is returning are always the same. If not then that function has the problem. I've looked around for that function but Google just keeps bringing me back to StackOverflow. Is it your own function or have you used the one from this question? MKMapView with address
Shouldn't it be in the first line something like this?
for (int i=0; i < [places count]; i++) {
or better just:
for (NSArray *arrayPlace in places) {
Related
I'm doing an RSVP reading project app where it blinks words on the screen. You can set the word chunk size (how many words you want displayed at a time) to either 1, 2, or 3. I got it working for 1 word by having my paragraph in a string and doing:
[self.textInput componentsSeparatedByString:#" ";
This makes me an array of words that I can use to blink one word at a time. How would I be able to do this with displaying 2 words at a time? Is there a way I can use this function again to do it differently, or should I iterate over this word array and make a new one with 2 word strings?
Any help or advice would be greatly appreciated as to what the best practice would to get this done. Thanks.
just like keith said create an array
NSArray *allwordsArray = [self.textInput componentsSeparatedByString:#" "];
Now you got all the info you need. Meaning you got the array with every word in it. Now its just a matter of putting it together. (I haven't tested this code)
NSMutableArray *twoWordArray = [[NSMutableArray alloc] init];
int counter=0;
for (int i=0; i<[allwordsArray count]; i++)
{
if (counter >= [allwordsArray count]) break;
NSString *str1 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
counter++;
if (counter >= [allwordsArray count]) break;
NSString *str2 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
NSString *combinedStr = [NSString stringwithformat#"%# %#", str1,str2];
[twoWordArray addObject: combinedStr];
counter++;
}
You have broken the string into components, which is on the right track. You could then make a smaller array that only includes components until you reach the chunk size. The final step would be to rejoin the string.
NSArray *components = [self.textInput componentsSeparatedByString:#" "];
NSRange chunkRange = NSMakeRange(0, chunkSize);
NSArray *lessComponents = [components subarrayWithRange:chunkRange];
NSString *newString = [lessComponents componentsJoinedByString:#" "];
I have an NSString that hold data (actually that could be presented an NSArray). and i want to output that on a label.
In NSLog my NSString output is:
(
"cristian_camino",
"daddu_02",
"_ukendt_babe_",
"imurtaza.zoeb"
)
What i want is, to present it like :"cristian_camino","daddu_02","_ukendt_babe_","imurtaza.zoeb"
In a single line.
I could accomplish that turning string to an array and do following: arrayObjectAtIndex.0, arrayObjectAtIndex.1, arrayObjectAtIndex.2, arrayObjectAtIndex.3.
But thats look not good, and that objects may be nil, so i prefer NSString to hold data.
So, how could i write it in a single lane?
UPDATE:
There is the method i want to use to set text for UILabel:
-(void)setLikeLabelText:(UILabel*)label{
//Likes
NSString* likersCount = [self.photosDictionary valueForKeyPath:#"likes.count"];
NSString* likersRecent = [self.photosDictionary valueForKeyPath:#"likes.data.username"];
NSString *textString = [NSString stringWithFormat:#"%# - amount of people like it, recent "likes": %#", likersCount, likersRecent];
label.text = textString;
NSLog(#"text String is %#", textString);
}
valueForKeyPath: returns an NSArray, not an NSString. Whilst you've declared likersCount and likersRecent as instances of NSString, they're actually both arrays of values. You should be able to do something like the following to construct a string:
NSArray* likersRecent = [self.photosDictionary valueForKeyPath:#"likes.data.username"];
NSString *joined = [likersRecent componentsJoinedByString:#"\", \""];
NSString *result = [NSString stringWithFormat:#"\"%#\"", joined];
NSLog(#"Result: %#", result);
componentsJoinedByString: will join the elements of the array with ", ", and then the stringWithFormat call will add a " at the beginning and end.
The statement is incorrect, the internal quote marks (" that you want to display) need to be escaped:
NSString *textString = [NSString stringWithFormat:#"%# - amount of people like it, recent \"likes\": %#", likersCount, likersRecent];
If somebody curious how i fix it, there it is:
for (int i =0; i < [likersRecent count]; i++){
stringOfLikers = [stringOfLikers stringByAppendingString:[NSString stringWithFormat:#" %#", [likersRecent objectAtIndex:i]]];
}
Not using commas or dots though.
i have implemented a search bar that searching trough an array of countries(presented in a picker view), the problem is that the user need to type the full country name that it will find it and i want him to be able to type even one letter and it will show the first country that starts with that letter and if types another than it sorts even further etc etc.
Anyone have any ideas??
for(int x = 0; x < countryTable.count; x++){
NSString *countryName = [[countryTable objectAtIndex:x]objectForKey:#"name"];
if([searchedStr isEqualToString:countryName.lowercaseString]){
[self.picker selectRow:i inComponent:0 animated:YES];
flag.image = [UIImage imageNamed:[[countryTable objectAtIndex:i]objectForKey:#"flag"]];
}
}
There's a method on NSArray called filteredArrayUsingPredicate: and a method on NSString called hasPrefix:. Together they do what you need...
NSString *userInput = //... user input as lowercase string. don't call this countryName, its confusing
NSPredicate *p = [NSPredicate predicateWithBlock:^BOOL(id element, NSDictionary *bind) {
NSString countryName = [[element objectForKey:#"name"] lowercaseString];
return [countryName hasPrefix:userInput];
}];
NSArray *filteredCountries = [countryTable filteredArrayUsingPredicate:p];
If you're on iOS 8 or OS X Yosemite, you can do:
NSString *country = countryName.lowercaseString; //"england"
NSString *needle = #"engl";
if (![country containsString:needle]) {
NSLog(#"Country string does not contain part (or whole) of searched country");
} else {
NSLog(#"Found the country!");
}
Else, if on versions below iOS 8:
NSString *country = countryName.lowercaseString; //"england"
NSString *needle = #"engl";
if ([country rangeOfString:needle].location == NSNotFound) {
NSLog(#"Country string does not contain part (or whole) of searched country");
} else {
NSLog(#"Found the country!");
}
Lastly, just iterate through all possible countries and apply this to them all. There might exist more robust solutions out there (like danh's solution with some smaller modifications), but this is by far the easiest to start with.
I have a chatroom that filters inappropriate content near perfectly using an array called blackList (an array of inappropriate words) and iterates through that and replaces inappropriate words with ****'s. However, when it comes across a word like "classy", it stars-out the third through fifth letter. How do I stop this and related problems? Thanks, and here is my code:
- (void)displayChatMessage:(NSString*)message fromUser:(NSString*)userName {
for(int i =0 ;i< blackList.count;i++)
{
NSMutableString* stars = [[NSMutableString alloc]init];
for(int j = 0 ; j <[[blackList objectAtIndex:i] length];j++)
[stars appendString:#"*"];
message = [message stringByReplacingOccurrencesOfString:(NSString*)[blackList objectAtIndex:i] withString:#"***"];
}
[chat appendTextAfterLinebreak:[NSString stringWithFormat:#"%#: %#", userName, message]];
[chat scrollToBottom:chat];
}
I'm trying to take an array of strings and take each item and put it into a string format. I wrote a method to do this as I need to concatenate the listed array values into another string. For some reason I cannot get the array values to list properly, a blank string is returned.
- (NSString*)listParameters:(NSArray*)params
{
NSString *outputList = #"";
if (params) {
for (int i=0; i<[params count]; i++) {
NSLog(#"%#",[params objectAtIndex:i]);
[outputList stringByAppendingString:[params objectAtIndex:i]];
if (i < ([params count] - 1)) {
[outputList stringByAppendingString:#", "];
}
}
}
NSLog(#"outputList: %#", outputList);
return outputList;
}
The first log statement properly returns a string (so there is definitely a string in the array), but the second log statement only returns "outputList: ".
I tried making outputList start as more than just an empty string which didn't work. I also tried assigning [params objectAtIndex:i] to a string then appending it, didn't work either.
I feel like I'm missing something obvious here, but I cannot get it to work.
How can I get this array of strings to print into a single string separated by commas?
You probably want to use a NSMutableString instead with its appendString method. NSString is immutable.
- (NSString*)listParameters:(NSArray*)params
{
NSMutableString *outputList = [[NSMutableString alloc] init];
if (params) {
for (int i=0; i<[params count]; i++) {
NSLog(#"%#",[params objectAtIndex:i]);
[outputList appendString:[params objectAtIndex:i]];
if (i < ([params count] - 1)) {
[outputList appendString:#", "];
}
}
}
NSLog(#"outputList: %#", outputList);
return outputList;
}
You need to assign the result of [outputList stringByAppendingString:[params objectAtIndex:i]] and [outputList stringByAppendingString:#", "] back to outputList.
It would be better still if you were using an instance of NSMutableString for outputList instead, as you're going to create a lot of autoreleased objects in that loop otherwise.
Try:
outputList = [outputList stringByAppendingString:#", "];
as stringByAppendingString works by returning a new String