0

I am trying to grab a string in my array, change nth letter in the string to a ?, then print the result in a textfield. The problem is my NSMutableArray is being stored into a UIPickerView and I think it would be best just to read the string from the PickerView then change the nth char and print the result. I am struggling with how to grab the string from the picker and change the nth letter.

    - (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
    {
    if (row == 0 ) {
     NSString *originalStringTwo = @"%@", *arrayDictionary;
    NSRange two = [originalStringTwo rangeOfString:@"2"];
    NSString *newStringTwo = [originalStringTwo stringByReplacingCharactersInRange:two        withString:@"?"];

    _resultLabel.text = newStringTwo;
    }


    if (row == 1 ) {
    NSString *originalStringThree = @"%@", *arrayDictionary;
    NSRange three = [originalStringThree rangeOfString:@"3"];
    NSString *newStringThree = [originalStringThreestringByReplacingCharactersInRange:three withString:@"?"];

    _resultLabel.text = newStringThree;
    }

    if ( row == 2 ) {
    NSString *originalStringFour = @"%@", *arrayDictionary;
    NSRange four = [originalStringFour rangeOfString:@"4"];
    NSString *newStringFour = [originalStringFour stringByReplacingCharactersInRange:four withString:@"?"];

   _resultLabel.text = newStringFour;
    }
    return 0;
}
0

1 Answer 1

0

That's not how you create an NSRange. Use NSMakeRange instead.

// Range from index 1 with length 1, eg. 2nd character:
NSRange two = NSMakeRange(2, 1);
NSString *newStringTwo = [originalStringTwo stringByReplacingCharactersInRange:two
                          withString:@"?"];

Note, that it's never a good idea to grab any data from a GUI element (except user input). What if the text field applies custom formatting?

You should keep your data in your array and use that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.