swipe directions and loop text string - iphone

Help me... I'm stuck. I'm trying to make simple game something like angry bird swipe. I don't know how to code this. I have two labels and swipe move code (up, down,left or right, any directions).
I want to transfer label1.text ("333333777777555555888888") on label2.text with shorter number ("3758"). So I can make Artificial Intelligence game better.
How do I do this?
Here's the code.
for (NSValue *point_value in TouchRecord) {
CGPoint current_point = [point_value CGPointValue];
NSUInteger idx = [self subregionIndexContainingPoint:current_point];
//collect string on label2
NSString *numValue = [[NSString alloc] initWithFormat:#"%d", idx];
label1.text = [label1.text stringByAppendingString:numValue];
}
Thanks.

I'm not entirely sure this is what you want but I think it is.
To shorten the string so that there are only one of each number use this function.
+(NSString*) removeDuplicateNumbersFromString:(NSString*)first{
//if first is "1112222334445" the return value of this function would be "12345"
NSString *end = [NSString stringWithString:#""]; //the return string
char last; //will track changes in numbers
for (int i = 0; i < [first length]; i++) {
char charAtIndex = [first characterAtIndex:i];
if (last != charAtIndex) {
//if the last character is different than the current character
//set the current character as last, and add that character to the return string
last = charAtIndex;
end = [end stringByAppendingFormat:#"%c",last];
}
}
NSLog(#"First:%#, End:%#",first,end); //prints out the start/end strings
return end;
}

Related

connecting strings

This is my code for connecting different NSNumbers from an array to one string. But for some reason I get a weird result in the watch area. The result of the NSLog is fine. Maybe you can help?
for (int i = 0; i < [ViewController sharedInstance].safeZonesUserList.count; i++) {
int userIndex = [[[ViewController sharedInstance].safeZonesUserList objectAtIndex:i] intValue];
NSString *arrayValue = [NSString stringWithFormat:#"%d",userIndex];
usersList = [usersList stringByAppendingString:arrayValue];
if (i < ([ViewController sharedInstance].safeZonesUserList.count - 1)) {
usersList = [usersList stringByAppendingString:#","];
}
}
My array holds 1 and 2, but the result I get in the watch area is: "1,2;\x8c\a#\x01\x03206"
Thanks,
You can replace your example with one line of code:
NSString* usersListAsString = [[ViewController sharedInstance].safeZonesUserList componentsJoinedByString:#","] ;
componentsJoinedByString gets the descriptions of every item in your NSArray, replacing the first 2 lines of code within your loop. Then it combines the descriptions into one NSString with your specified string (",") in-between them, replacing your example's lines 3 - 5.

keyboard stuck when searching in uitableview using uisearchbar...?

I have a uitableview and uisearchbar. I'm binding table with tons of records and give functionality to user can search from that. But many times when data size is large keyboard stuck uptill it reload the table. Is it anyway so I can asynchronously search. It's iOS 4 app.
my uisearchbar textDidChange method is as below
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if(searchText.length == 0)
{
isFiltered = FALSE;
}
else
{
#autoreleasepool {
isFiltered = true;
membersMFilterArray = [[NSMutableArray alloc] init];
int lineCount = [[[membersMArray objectAtIndex:0] valueForKey:#"LineCount"] intValue];
NSString *membersMArrayValue;
for (int i=0; i<[membersMArray count]; i++)
{
membersMArrayValue = [membersMArray objectAtIndex:i];
NSString *line;
for (int j=0; j<lineCount; j++)
{
line = [NSString stringWithFormat:#"Line%d",j+1];
NSRange lineRange = [[membersMArrayValue valueForKey:line] rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(lineRange.location != NSNotFound)
{
[membersMFilterArray addObject:[membersMArray objectAtIndex:i]];
break;
}
}
}
}
}
[tblMember reloadData];}
my array is as below from which I've search, Actually this is one user data which arrise in one table row and if there is 10000 rows then multiply with this. So above for loop is I think as because of this way.
(
{
Line1 = "Ashish";
Line10 = "Ahmedabad";
Line11 = "Gujarat";
Line12 = "";
Line13 = "India";
Line14 = "";
Line15 = "";
Line16 = "abc#yahoo.com";
Line17 = "";
Line18 = "";
Line19 = "xyz";
Line2 = "Ahmedabad, Gujarat";
Line20 = "Jun 04, 2012";
Line3 = "";
Line4 = "";
Line5 = "";
Line6 = "abc";
Line7 = "xyz";
Line8 = "";
Line9 = "";
LineCount = 20;
"Member_id" = GM00018004;
RowNo = 01;
}
)
The short answer is yes. You can perform the search asynchronously, say, with Grand Central Dispatch. You can then notify the datasource of your table view to update.
However, it would be much better to make your search more efficient and achieve acceptable real time updates. How to achieve this, depends on how your data is stored and indexed. For example, if you are using Core Data you would have to tweak your NSFetchedResultsController and maybe construct a new entity with single strings of words occurring in your longer strings to be searched.
Looking at your search code, you have two nested loops - an almost certain recipe for performance degradation. Maybe you should consider some of Apple's automatic iteration mechanisms, (there are still iterations, but behind the scenes) such as with key paths or predicates. For instance, something like:
resultArray = [membersArray filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:#"self CONTAINS[cd] %#", searchText]];
From your code, i think the two for loops are unnecessary. Use better concept to sort the array items.
Some suggestions from my part to speedup.
Remove the auto-release-pool
Use one for-loop instead of two
You can check it out the start & end time of execution using NSlog.Discover the time taken process and fix it.
Example:
NSLog(#"loop1 started now")
for (int i=0; i<[membersMArray count]; i++)
{
membersMArrayValue = [membersMArray objectAtIndex:i];
NSString *line;
for (int j=0; j<lineCount; j++)
{
NSLog(#"loop2 started now")
line = [NSString stringWithFormat:#"Line%d",j+1];
NSRange lineRange = [[membersMArrayValue valueForKey:line] rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(lineRange.location != NSNotFound)
{
[membersMFilterArray addObject:[membersMArray objectAtIndex:i]];
break;
}
}
NSLog(#"loop2 ended now")
}
NSLog(#"loop1 ended now")

How to know the length of NSString that fits a UILabel with fixed size?

I know NSString has methods that determine the frame size for it, using NSString UIKit Additions, sizeWithFont......
How about the other way around? I mean if I have a fixed frame size, how do I know how many characters or words for a NSString that can fit into it?
If I know this, I can cut off the NSString easily.
thanks
It might not be the most elegant solution, but you could do something like this:
- (NSString *)string:(NSString *)sourceString reducedToWidth:(CGFloat)width withFont:(UIFont *)font {
if ([sourceString sizeWithFont:font].width <= width)
return sourceString;
NSMutableString *string = [NSMutableString string];
for (NSInteger i = 0; i < [sourceString length]; i++) {
[string appendString:[sourceString substringWithRange:NSMakeRange(i, 1)]];
if ([string sizeWithFont:font].width > width) {
if ([string length] == 1)
return nil;
[string deleteCharactersInRange:NSMakeRange(i, 1)];
break;
}
}
return string;
}
Then call it like this:
NSString *test = #"Hello, World!";
CGFloat width = 40.0;
UIFont *font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
NSString *reducedString = [self string:test reducedToWidth:width withFont:font];
NSLog(#"%#", reducedString);
You cannot know/determine the number of characters that fits in a UILabel with fixed width because some characters are smaler than others, eg l and m.
There are two options:
Use Mono-Space-Fonts (each character has also a fixed width). Then determine the width for one char in your font with your font-size and calculate the number of chars
Allow any number of characters and check on insert if the inserted characters fit.
You have to know what behaviour you want to have. What should happen if there is text that does not fit. If you only want to truncate (like the solution of mortenfast does) then just use UILineBreakModeTailTruncation for the lineBreakMode-property of your UILabel (there are more options, like TruncateHead, Clip, Word Wrap)
Or you just just use the lineBreak property and set it to NSLineBreakByCharWrapping and move on with your life. https://stackoverflow.com/a/29088337/951349
Thanks #Morten. I've updated the sample code to handle word separation. It also eliminates extra spaces in between words. It has not been tested in the field, but my tests have, thus far, proven OK. Please update at your leisure if you find improvements or bug/glitch fixes.
-(NSString*)string:(NSString*)sourceString reducedToWidth:(CGFloat)width withFont:(UIFont*)font {
// if full string is within bounds, simply return the full string
if( [sourceString sizeWithFont:font].width <= width ) return sourceString;
// break up string into words. if <= 1 word, return original string
NSArray* words = [sourceString componentsSeparatedByString:#" "];
NSInteger numWords = [words count];
if( numWords <= 1 ) return sourceString;
// our return var. we populate as we go
NSMutableString* str = [NSMutableString string];
// temp var to test with before adding to return string
NSMutableString* strTemp = [NSMutableString string];
// string to hold word LESS spaces
NSString* strWordTemp = nil;
// the word we're currently on
NSInteger numWord = 0;
// whether we need to add a space (when not last word)
Boolean addSpace = NO;
// loop through our words....
for( NSString* strWord in words ) {
// which word we're on
numWord++;
// eliminate white space
strWordTemp = [strWord stringByReplacingOccurrencesOfString:#" " withString:#""];
// if this word is empty or was a space(s), skip it
if( [strWordTemp isEqualToString:#""] ) continue;
// append to temp string
[strTemp appendString:strWordTemp];
// if we're still within the bounds...
if( [strTemp sizeWithFont:font].width <= width ) {
// default = no extra space
addSpace = NO;
// if we're not the last word, add a space & check for length
if( numWord < numWords ) {
[strTemp appendString:#" "];
// if adding space made it too long, then just don't add it!
if( [strTemp sizeWithFont:font].width > width ) {
// it was too long with space, so we'll just add word
[str appendString:strWordTemp];
break;
}
// otherwise, it's OK to add the space
else addSpace = YES;
}
// append to return string and continue
[str appendFormat:#"%#%#", strWordTemp, ( addSpace ? #" " : #"" )];
}
// otherwise, we're done
else break;
}
// return our result
return str;
}

stringByReplacingCharactersInRange don't replace It's Append !

I Spent 5 hours try to figure a way for that..i'm trying to do a hangman app for iphone and the method below is the method that should be called when the player chooses a character and it match the chosen word..
-(void)replaceTheHiddenTextWithNewText:(NSString*)character{
NSString *fullTextField = fullText.text;
int textCount = [hiddenText.text length];
NSString *theRiddle;
for (int i = textCount-1 ; i>=0; i--) {
NSString *hiddenTextField = [[NSMutableString alloc] initWithString:hiddenText.text];
NSString *aChar=[fullTextField substringWithRange:NSMakeRange(i/3,1)];
if ([aChar isEqualToString:#" "]) {
theRiddle= [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#" "];
}else if ([aChar isEqualToString:character]) {
theRiddle =[hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:aChar];
}else{
theRiddle = [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#"_"];
}
hiddenTextField = theRiddle;
}
hiddenText.text=theRiddle;
}
the problem is stringByReplacingCharactersInRange doesn't replace the character, it appends it to the underscore what am I doing wrong here?
Best Regards,
M Hegab
Just played around with your code. It does not work, but stringByReplacingCharactersInRange is not your problem.
Your game logic doesn't work like it should. Get a pen and a sheet of paper and "manually" loop through your for loop to see that this must be wrong.
Next time, if you've stared at code for half an hour, take a pen. This will save you at least 4 hours :-)
There are some issues with your code. Assume Kartoffelkäfer is the word you are looking for, and the user enters the letter f.
for (int i = textCount-1 ; i>=0; i--) {
NSString *hiddenTextField = [[NSMutableString alloc] initWithString:hiddenText.text];
// you are creating this string in every loop from the text of a (I guess) UITextField.
// I don't know what the content of this text is but I guess it is suppossed to be `______________`
// in every loop you replace the word where you replaced the _ with the correct letter with the string from the textfield.
// Btw, you are leaking this string.
NSString *aChar=[fullTextField substringWithRange:NSMakeRange(i/3,1)];
// Kartoffelkäfer has 14 chars so i is 13. And 13/3 is 4. And the character at index 4 is o
// In the next loop i is 12. And 12/3 is 4, too.
// next three loops will give you index 3. Then you get three times index 2, and so one.
// you never reach the letter f, anyway.
if ([aChar isEqualToString:#" "]) {
theRiddle= [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#" "];
}else if ([aChar isEqualToString:character]) {
theRiddle =[hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:aChar];
}else{
theRiddle = [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#"_"];
// You should not replace a unmatched character with a _ . Because already matched letters would be overwritten.
}
hiddenTextField = theRiddle;
}
I assumed that the content of hiddenText.text is #"______"
and the content of fullText.text is #"Kartoffelkäfer". So hiddentext is the exact length as the fullText.
What I had to change to get this to work:
NSString *theRiddle;
NSString *hiddenTextField = [[[NSMutableString alloc] initWithString:hiddenText.text] autorelease];
for (int i = textCount-1 ; i>=0; i--) {
NSString *aChar=[fullTextField substringWithRange:NSMakeRange(i,1)];
if ([aChar isEqualToString:#" "]) {
theRiddle= [hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#" "];
}else if ([aChar isEqualToString:character]) {
theRiddle =[hiddenTextField stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:aChar];
}
else {
theRiddle = hiddenTextField;
}
hiddenTextField = theRiddle;
}
hiddenText.text=theRiddle;
Far away from good code, but I tried to change your code as little as possible.

Why doesn't this for loop execute?

I have a picker view controller to select a chemical source and possibly a concentration. If the source doesn't have concentrations, it just presents a single picker. It gets populated by an NSDictionary with source type names as keys and a custom model object I made called Chemical that has four properties, two NSString, one float and one BOOL.
When I trigger this with dictionary that has 2 components, I want to extract the four values from the Chemical that is represented. Note that I populate the picker with values from the first two properties, but not the float or BOOL. I run through the array for the key that's selected in the first component and check the string from the second component against the chemConcentration property from each of the Chemicals in the key/value array. When the chemConcentration matches, I know I have the right Chemical and I can get its properties to send back.
Whew!
The problem is that even though I know I get to the for loop, it seems to get skipped. The NSLog right before it prints, but the one inside doesn't. sourceConstant and sourceIsLiquid stay 0.0 and NO
- (IBAction)selectedSourceButton {
NSLog(#"selectedSourceButton pressed");
NSInteger sourceRow = [picker selectedRowInComponent:kSourceComponent];
NSString *selectedSource = [self.sources objectAtIndex:sourceRow];
NSArray *selectedChemicalGroup = [dictionaryOfSources objectForKey:selectedSource];
NSInteger concentrationRow = [picker selectedRowInComponent:kConcentrationComponent];
NSString *selectedConcentration = [[NSString alloc] init];
float selectedConstant = 0.0;
BOOL selectedIsLiquid = NO;
if (numberOfComponents == 2) {
NSLog(#"numberOfComponents = 2 if/then chosen"); // <-- This prints.
selectedConcentration = [self.concentrations objectAtIndex:concentrationRow];
NSLog(#"begin selectedConcentration for loop. Number of loops = %d", [selectedChemicalGroup count]); // <-- And so does this.
for (int i; i<[selectedChemicalGroup count]; i++) { // <-- But this doesn't seem to fire!
NSLog(#"selectedConcentration = %#, from selectedChemicalGroup = %#", selectedConcentration, [[selectedChemicalGroup objectAtIndex:i] chemConcentration]); // <-- Because this doesn't print.
if ([selectedConcentration isEqualToString:[[selectedChemicalGroup objectAtIndex:i] chemConcentration]]) {
selectedConstant = [[selectedChemicalGroup objectAtIndex:i] chemConstant];
selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:i] chemIsLiquid];
}
}
}
else {
selectedConcentration = #"";
selectedConstant = [[selectedChemicalGroup objectAtIndex:0] chemConstant];
selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:0] chemIsLiquid];
}
NSLog(#"selectedSourceButton source to return = %#, concentration = %#, sourceConstant = %1.7f, isLiquid = %d", selectedSource, selectedConcentration, selectedConstant, selectedIsLiquid);
if ([self.delegate respondsToSelector:#selector (sourcePickerViewController:didSelectSource:andConcentration:andConstant:andIsLiquid:)]) {
[self.delegate sourcePickerViewController:self didSelectSource:selectedSource andConcentration:selectedConcentration andConstant:selectedConstant andIsLiquid:selectedIsLiquid];
}
}
You need to initialize your variable i: for (int i = 0; ...
But there's a better way to do this, using "fast enumeration":
for (MyChemicalGroupClass *group in selectedChemicalGroup) {
if ([selectedConcentration isEqualToString:[group chemConcentration]]) {
...
}
}
Initialize loop count i
for (int i = 0; i<[selectedChemicalGroup count]; i++)
Do the following and you will understand why:
int i;
NSLog(#"%d", i);