I would like to delete multiple spaces from self.textDocumentProxy when working with my extension's KeyboardViewController, and was wondering if there is a Apple-supported method that specifically performs this action?
So far, I have been using a pretty "hacky" way of doing the following (here it deletes all the previous characters found on textDocumentProxy):
for (int i = 0; i < self.textDocumentProxy.documentContextBeforeInput.length; i++){
[self.textDocumentProxy deleteBackward];
}
The issue with this lies in the method deleteBackward, which, depending on what prompt is given, always deletes around half (its quite reliable, especially when documentContextBeforeInput is longer than 20 characters) of the total number of times its told to delete. Since this is rather unreliable, I was wondering if there is a way to easily delete multiple spaces, or all of the texted in textDocumentProxy.documentContextBeforeInput
Thanks!
There is a fundamental issue in the loop you're using:
for (int i = 0; i < self.textDocumentProxy.documentContextBeforeInput.length; i++){
[self.textDocumentProxy deleteBackward];
}
The i < self.textDocumentProxy.documentContextBeforeInput.length check is performed multiple times. And the .length attribute is actually decreasing by 1 for every deleteBackward you do. i however is merrily increasing by 1 for each iteration.
As a result only half will be deleted.
You can flip the order around to fix the issue.
for (int i = self.textDocumentProxy.documentContextBeforeInput.length; i > 0; i--){
[self.textDocumentProxy deleteBackward];
}
You can also cache the original length of the textDocument before you start changing it.
Maybe try this solution that will erase everything in the input text, not only the text before the cursor ;)
func deleteInputText() {
if let afterInput = self.textDocumentProxy.documentContextAfterInput {
self.textDocumentProxy.adjustTextPositionByCharacterOffset(afterInput.characters.count)
}
while let _=self.textDocumentProxy.documentContextBeforeInput {
self.textDocumentProxy.deleteBackward()
}
}
while (self.textDocumentProxy.hasText==YES)
{
[self.textDocumentProxy deleteBackward];
}
should remove all the text.
[self.textDocumentProxy deleteBackward]; deletes only 1 character.
Related
I'm new to mql4 and am confused with the basics. I want to prepare an exit strategy. Here are the conditions. If it's a buy trade and we have to sell for closing the order:
The candle should give a close below the Supertrend.
Next candle should cut the low of the previous candle.
Below is the part of the code I've prepared.
i=1;
if (Close[i]<st)
{
low=close[1];
a=checkt1();
if (a==True)
{
OrderClose()
}
}
else if(Close[i]>st)
{
return(EMPTY_VALUE);
}
bool check1t()
{
if (Ask<a && Bid<a)
{
CloseOrder();
}
return True
}
Here the value of close keeps changing as I have set it to close[1]. Is there any function or any way that can store the value of close of the candle that had cut supertrend only? And not take up any other values?
so you need to check that previous candle is below the one before, and that candle before closed below the indicator.
`void Check4closeBuy(){
double low1 = iLow(_Symbol,0,1), low2 = iLow(_Symbol,0,2),
close2 = iClose(_Symbol,0,2), ind2 = iCustom(_Symbol,0,ind_name,***,buffer,2);
if (ind2>close2 && low2>low1){
//close here
}
}`
about comparing doubles - since they are doubles not integers it is better to compare the difference below half-tick or something like that, but need to be careful with that.
so in basic case: double1>double2 -> double1-double2>Point/2
but it depends on your indicator, it can have _Digits or more (like different MA may have more digits after dot then just 5).
I was writing pretty complicated UI tests using XCTest, and recently switched to EarlGrey, because it's so much faster and way more reliable - Tests aren't randomly failing on a build server, and the test suite could take up to a half hour to run!
One thing that I haven't been able to do in EarlGrey, that I could do in XCTest, was randomly select an element.
For example, on a calendar collectionView, I could query for all collectionViewCells with 'identifier' using an NSPredicate, and then randomly select a day using [XCUIElementQuery count] to get an index, and then tap.
For now, I'm going to hard code it, but I would love to randomize date selection so that I don't have to rewrite tests if we change app code.
Please let me know if I can elaborate, looking forward to solving this!
Step 1 write a matcher that can count elements matching a given matcher using GREYElementMatcherBlock:
- (NSUInteger)elementCountMatchingMatcher:(id<GREYMatcher>)matcher {
__block NSUInteger count = 0;
GREYElementMatcherBlock *countMatcher = [GREYElementMatcherBlock matcherWithMatchesBlock:^BOOL(id element) {
if ([matcher matches:element]) {
count += 1;
}
return NO; // return NO so EarlGrey continues to search.
} descriptionBlock:^(id<GREYDescription> description) {
// Pass
}];
NSError *unused;
[[EarlGrey selectElementWithMatcher:countMatcher] assertWithMatcher:grey_notNil() error:&unused];
return count;
}
Step 2 Use % to select a random index
NSUInteger randomIndex = arc4random() % count;
Step 3 Finally use atIndex: to select that random element and perform action/assertion on it.
// Count all UIView's
NSUInteger count = [self elementCountMatchingMatcher:grey_kindOfClass([UIView class])];
// Find a random index.
NSUInteger randIndex = arc4random() % count;
// Tap the random UIView
[[[EarlGrey selectElementWithMatcher:grey_kindOfClass([UIView class])]
atIndex:randIndex]
performAction:grey_tap()];
How to compare two strings while some parts of them are same?
Let say I have a string ABCAAAA.
For some reason, only ONE character of the string ABCAAAA can be changed at a time. For example, I can change this string to DBCAAAA.
Now the problem is :
How can I ensure ONLY ONE character is changed each time? Is there a method for NSString to compare how much two strings differ?
Purpose: I put each string into own UITextField to determine whether this one is editable if others had changed. I need to ensure only one is edited at a time. So if one had been edited, I will set UITextField's enable to NO to disable editing.
There is no built-in NSString method available to do what you want. You need to write your own method. Objective-C does let you "extend" classes with new methods to cover cases like this.
This is how I would do it:
#interface NSString(Extend)
-(NSInteger)proximity:(NSString*)otherString;
#end
#implementation NSString(Extend)
-(NSInteger)proximity:(NSString*)otherString
{
NSUInteger length = [otherString length];
if(length != [self length]) return -1;
NSUInteger k;
NSUInteger differences = 0;
for(k=0;k<length;++k)
{
unichar c1 = [self characterAtIndex:k];
unichar c2 = [otherString characterAtIndex:k];
if(c1!=c2)
{
++differences;
}
}
return differences;
}
#end
Then in my code at the place I wanted to check I would say something like
Michael L gave a good answer (+1)
I just wanted to note that if all your text strings are in separate UITextFields, then only one of them can be edited at a time. Therefore I really don't understand what you want to do with enable = NO part.
If text strings must be edited in certain order, just keep count of order by index by yourself.
I have a question regarding integers outside of methods in Objective-C/Xcode. I'm trying to create a simple guessing game, however my randomizer randomize number every time when the method is called, here is the code snipped:
- (IBAction)guessButton:(id)sender {
int tempUserGuess = [self.textField.text integerValue];
int randomNumber = (arc4random() % 11);
if(tempUserGuess == randomNumber){
self.guessAns.text = #"you won!";
}
if (tempUserGuess < randomNumber){
self.guessAns.text = #"no! too low!";
}
if (tempUserGuess > randomNumber){
self.guessAns.text = #"no! too high!";
}
}
The reason why I'm trying to put an int outside of the method is of that once randomized integer should not be randomized every single time (of course). By the way, everything works fine, the app compiles and works but every single time when I hit return, it randomizes the number.
I know how to do this in Java, but Objective-C seems to be more complex.
Your guessButton method is probably a member of some class. You need to add property to that class holding that randomized number.
You only have to create/store a random number once per game play. There is no need to call the randomize method each time the guess button is pressed. The guessButton method is basically like an ActionListener in java. Each time the button is pressed, whatever's inside the curly braces will be executed. If you add a play again button to the game, then you might want to call the randomize method inside of it's action method.
if(tempUserGuess == num){
self.guessAns.text = #"you won!";
}
This will be a good way because it will be using less memory in the sytem when trying to divert the random number. Good work
Okay, I got it.
Here is the answer:
in ViewController.h I had to include:
NSInteger num;
and then in ViewController.m a method that simply returns a number:
-(int)giveRandom {
int randomNumber = (arc4random() % 10);
return randomNumber;
}
And then refer to 'num' in the method with if statements such as:
...
if(tempUserGuess == num){
self.guessAns.text = #"you won!";
}
...
For some reason it returns 0, but I will try to solve it.
Thanks!
I need a way to tell when the deslect count is equal to 0 an even amount of times. For example, if the count ==0 once dont do anything but if equals 0 twice then call the deselect nslog function. What is the best current way to make this work?
if([self.mapView.selectedAnnotations count] == 0){
NSLog(#"DE SELECT");
}
As long as you're always going to do the same thing on odd selections and the same other thing on even selections, just use a static BOOL.
if([self.mapView.selectedAnnotations count] == 0){
static BOOL odd = YES;
if(odd) {
//do something
} else {
//do something else
}
odd = !odd
}
Since your code needs to react to the user deselecting annoationations, you probably need to implement the mapView:didDeselectAnnotationView: method and add some logic that checks to see if the selection count has dropped to zero. If it has, increment a counter.