Replacing multiple strings with Objective-C - objective-c

I am currently doing an experiment where I input text into a UITextField, and the text is searched to see if it has certain strings in it. If it finds the certain text, it should replace it, and send it to a UITextView. (Think of a super-simplified translator)
The problem I am having with it is that it only sends the text I last paired.
For exammple,
NSString *mainString = [[NSString alloc] initWithString:field.text];
NSArray *stringsToReplace = [[NSArray alloc] initWithObjects:#"The",#"dog",#"cried", nil];
NSArray *stringsReplaceBy = [[NSArray alloc] initWithObjects:#"ehT",#"god",#"deirc", nil];
for (int i=0; i< [stringsReplaceBy count]; i++)
{
look.text = [mainString stringByReplacingOccurrencesOfString:[stringsToReplace objectAtIndex:i] withString:[stringsReplaceBy objectAtIndex:i]];
}
When I type in, "The dog cried." it should be saying "ehT god deirc." However, it is responding with "The dog deirc."
Please help.

You are calling stringByReplaceingOccurencesOfString three times on the same string:
for (int i=0; i< [stringsReplaceBy count]; i++)
{
look.text = [mainString stringByReplacingOccurrencesOfString:[stringsToReplace objectAtIndex:i] withString:[stringsReplaceBy objectAtIndex:i]];
}
Instead, save the result into another string object:
NSString *modifiedString = mainString;
for (int i=0; i< [stringsReplaceBy count]; i++)
{
modifiedString = [modifiedString stringByReplacingOccurrencesOfString:[stringsToReplace objectAtIndex:i] withString:[stringsReplaceBy objectAtIndex:i]];
}
look.text = modifiedString;

Related

How to add elements in NSArray in Objective-C?

I want to add multiple elements in NSArray with #"title" and value.
for(int i = 0; i < size; i++){
BSDevice *d=[[DeviceManager devices] objectAtIndex:i];
self.dataArray =#[#{#"title":d.Name}];
}
But in this code it only adds one value. When I try with this method it doesn't work:
for(int i = 0; i < size; i++){
BSDevice *d=[[DeviceManager devices] objectAtIndex:i];
[self.dataArray addObject:#[#{#"title":d.buddyName}]];
}
I want to have multiple values with string and its value in NSArray like this #"title":d.Name,#"title":d.Name,#"title":d.Name
Your question isn't very clear and you didn't really explain what problem you are having with the second bit of code, but I think what you want is the following:
for (BSDevice *d in [DeviceManager devices]) {
[self.dataArray addObject:#{ #"title" : d.buddyName }];
}
This assumes that self.dataArray is an NSMutableArray and somewhere before this for loop you initialized self.dataArray as:
self.dataArray = [NSMutableArray array];

Why does replaceObjectAtIndex depend on whether or not I use a new definition in the loop?

I have two codes. Not working is the following:
NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
for (int i=0; i<[dataSetArray count]; i++) {
tmpArray = (NSMutableArray *) [dataSetArray objectAtIndex:i];
// OR use: tmpArray = dataSetArray[i]
... doing stuff
[tmpArray replaceObjectAtIndex:0 withObject:tmpStr];
}
While this works:
for (int i=0; i<[dataSetArray count]; i++) {
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithArray:[dataSetArray objectAtIndex:i]];
... doing stuff
[tmpArray replaceObjectAtIndex:0 withObject:tmpStr];
}
Two questions:
The first code doesn't yield an NSMutableArray. Why? I declare it
above.
Is there a better way to obtain the same result. I just
dislike defining variables in a loop. This makes the code
unreadable.
--- edit:
Here the full code:
Datatypes are:
dataSetArray: NSMutableArray. However, its contents (i.e. dataSetArray[i]) are NSArrays (I read them into the program from an excel file).
NSString *tmpStr = [[NSString alloc] init];
for (int i=0; i<[dataSetArray count]; i++) {
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithArray:[dataSetArray objectAtIndex:i]];
for (int j=0; j<[tmpArray count]; j++) {
if ( [dataSetArray[0][j] isEqualToString:#"Number"] ) {continue;}
tmpStr = (NSString *) [tmpArray objectAtIndex:j];
// replace single backslash by double-backslash:
tmpStr = [tmpStr stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"];
// replace first dollar sign by "<p>\\[" and second by "\\]</p>"
// Use a methode defined in the NSString+Extension
tmpStr = [tmpStr replaceTexFormulaSigns:tmpStr];
//NSLog(#"j=%d", j);
//NSLog(#"tmpArray is of type: %#", [tmpArray class]);
//NSLog(#" tmpStr is of type: %#", [tmpStr class]);
[tmpArray replaceObjectAtIndex:j withObject:tmpStr];
}
[dataSetArray replaceObjectAtIndex:i withObject:tmpArray];
}
So even if I use your suggestion, I am still facing the same problem with the inner array.
The first code doesn't yield a NSMutableArray. Why? I declare it above.
The declaration of the reference variable tmpArray does not change the type of the referred object. It is still an (immutable) array.
The creation of the mutable array at the very beginning of the first snippet is without any meaning, because the reference to it is overridden.
Is there a better way to obtain the same result. I just dislike defining variables in a loop. This makes the code unreadable.
Yes. The second example works in a way, but do something completely different. (It always creates a new array with a single item in it. No, that's not true. It shouldn't compile at all.)
You should do:
NSMutableArray *tmpArray = [dataSetArray mutableCopy];
for (int i=0; i<[dataSetArray count]; i++)
{
…
[tmpArray replaceObjectAtIndex:i withObject:tmpStr];
}
You should really get some additional knowledge about objects and object references.

I need to use intermediate Array in loop

I have a basic question. I need to use intemediate array in loop. This is my code:
NSMutableArray *interArray = [[NSMutableArray alloc] initWithCapacity:2];
NSMutableArray *finalyArray = [[NSMutableArray alloc] init];
NSInteger a;
for(int j=0; j<5; j++)
{ [interArray removeAllObjects];
for(int i=0; i<2; i++)
{
a = [array ObjectAtInde:i]; // this is existing array
[interArray addObject:a];
}
[finalyArray addObject:interArray];
}
but when i delete objects in intermediate array I get empty Arrays in finaly Array. How to fix it? Thanks
The reason it won't work as you expect is because interArray is a reference to an object which you empty and fill in the loop and then add to the finalyArray, however you are adding the same intermediate array each time.
What you want to do it create a new intermediate array within the loop and therefore each intermediate array will be a separate object:
NSMutableArray *finalyArray = [[NSMutableArray alloc] init];
NSInteger a;
for(int j=0; j<5; j++)
{
NSMutableArray *interArray = [[NSMutableArray alloc] initWithCapacity:2];
for(int i=0; i<2; i++)
{
a = [array ObjectAtIndex:i]; // this is existing array
[interArray addObject:a];
}
[finalyArray addObject:interArray];
}
(Note this code won't work anyway as you cannot store NSInteger in a collection class without first wrapping it in a NSNumber object).

divide string into characters

I have an NSString *titleName which changes according to an if statement. So the length (number of characters) in the string changes. I would like to divide titleName into a MutableArray of separate strings consisting of its individual characters. I would then like to use these separate strings as the text in different UILabels. I am not sure as how to go about this.
Through some research I have tried to create the NSMutable array like this
NSMutableArray *letterArray = substringWithRange:((i = 0);i<[titleName2 length];i++));
but this gives me an error Use of undeclared identifier 'substringWithRange.
Can someone help me.
I decided to use componentsSeparatedByString instead and just created my various strings with a , between each letter. Thanks for anybody's thoughts though.
The code you pasted is not valid objective-C.
To keep the same algorithm you should write something like :
NSMutableArray *letterArray = [NSMutableArray array];
NSUInteger length = [titleName2 length];
for (NSUInteger i = 0; i < length; ++i) {
[letterArray addObject:[titleName2 substringWithRange:NSMakeRange(i, 1)]];
}
It's probably much "cheaper" to hold a C-array of unichar characters that make-up the string. It will also be quicker to create:
NSString *input = #"How now brown cow?";
unichar chars[[input length]];
for (NSInteger i = 0; i < [input length]; i++)
chars[i] = [input characterAtIndex:i];
Alternatively you could use malloc() to create the C-array:
NSString *input = #"How now brown cow?";
unichar *chars = (unichar *)malloc([input length]);
for (NSInteger i = 0; i < [input length]; i++)
chars[i] = [input characterAtIndex:i];
and then use free(), later, to, err, free the memory:
free(chars);
Cheaper still, would be to not split-up the string at all...
Try this below code
NSMutableArray *letterArray = [NSMutableArray array];
for (int i = 0;i<[titleName2 length];i++)
{
[letterArray addObject: [titleName2 substringWithRange:NSMakeRange(i,1)]];
}
DLog(#"%#", letterArray);
Other option to get characters of string
NSMutableArray *letterArray = [NSMutableArray array];
for (int i=0; i < [titleName2 length]; i++)
{
[letterArray addObject:[NSString stringWithFormat:#"%c", [titleName2 characterAtIndex:i]]];
}
DLog(#"characters - %#", letterArray);

Show object in NSMutableArray of matrix

Following this code:
In ViewController.m
double kk[2][2] = {{1,2},{5,6}};
if (!matrix1Col) {
matrix1Col = [NSMutableArray array];
}
for (int i=0; i<2; i++) {
[matrix1Row removeAllObjects];
if (!matrix1Row) {
matrix1Row = [NSMutableArray array];
}
for (int j=0 ; j<2; j++) {
[matrix1Row insertObject:[NSNumber numberWithDouble:kk[i][j]] atIndex:j];
}
[matrix1Col insertObject:matrix1Row atIndex:i];
}
self.label100.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:0] objectAtIndex:0] doubleValue]];
self.label110.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:1] objectAtIndex:0] doubleValue]];
self.label101.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:0] objectAtIndex:1] doubleValue]];
self.label111.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:1] objectAtIndex:1] doubleValue]];`
I wanna show object in NSMutableArray of matrix which receive value from matrix of double in label.
And my all label must show as following ->label100 show 1 ->label110 show 5 ->label101 show 2 and ->label111 show 6
but It shows ->label100 show 5 ->label110 show 5 ->label101 show 6 and ->label111 show 6
How can I do?
The problem is that you're always overwriting already inserted objects, as you insert the row many times in the same loop. That's how I would write it:
double kk[2][2] = {{1,2},{5,6}};
NSMutableArray* matrix= [NSMutableArray new];
for(unsigned int i=0; i<2; i++)
{
NSMutableArray* row= [NSMutableArray new];
for(unsigned int j=0; j<2; j++)
{
[row addObject: #(kk[i][j]) ];
}
[matrix addObject: row];
}