Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a string with string in ios

Tags:

iphone

I am in a problem with some text sharing to evernote,sharing evernote is success,but here is my current situation with code. I have a UITableView which has some text and title for that corresponding text. When the share button is clicked it will share the text onebyone to evernote website, but the title remains static. There I get the first title name along with diffrent text. My code for this is in my tableview in rowAtIndexPath

NSMutableString *strr=[[NSMutableString alloc]initWithString:[appDelegate.indexArray objectAtIndex:indexPath.section]];
cell.textLabel.text =strr ;
cell.textLabel.text = [appDelegate.indexArray objectAtIndex:row];
cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:14.0]; 
cell.textLabel.textColor = [UIColor brownColor];


[appDelegate.notesArray objectAtIndex:row]];
//cell.detailTextLabel.text =notes;
cell.detailTextLabel.font = [UIFont fontWithName:@"Georgia" size:14.0]; 
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.text = [appDelegate.notesArray objectAtIndex:row];

appDelegate.indexArray is the title content for each cell and appDelegate.notesArray has the textnote for the corresponding titles.

In shareButton click:

 NSMutableString *str = [[NSMutableString alloc] initWithString:@"NOTES:"]; 
 for (int i = 0; i<[appDelegate.notesArray count]; i++) { 
        NSString * aString = [[NSString alloc] initWithString:[appDelegate.notesArray objectAtIndex:i]] ;
        NSString * ENML= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%@",aString];

    ENML = [NSString stringWithFormat:@"%@%@", ENML, @"</en-note>"];
    NSLog(@"%@", ENML);

    // Adding the content & resources to the note
    [note setContent:ENML];

This will give the one by one upload of notetext.but for title I include this code

NSMutableString *strtitle = [[NSMutableString alloc] initWithString:@"myBibleApp"]; 
    for (int i = 0; i<[appDelegate.indexArray count];i++ ) { 
        NSString * aStringtitle = [[NSString alloc] initWithString:[appDelegate.indexArray objectAtIndex:i]] ;
       /* NSString *ENMLtitle = [NSString stringWithFormat:@"%@%@", aStringtitle];
        NSLog(@"%@", ENMLtitle);*/

    note.title = aStringtitle;

But here is my problem it uplode the title and text in double. That means, I have one text with title. When I click the sharebutton it uploads two times.1=2,2=4,3=6 like that. Nut only addding the title I get this problem. If I put the title static,note.title =@"statictitle". It will not repeat the upload. How can I append the string in correct way? Please help me. Thanks in advance.

like image 687
stackiphone Avatar asked Feb 15 '12 09:02

stackiphone


People also ask

Can you use += for string concatenation?

The same + operator you use for adding two numbers can be used to concatenate two strings. You can also use += , where a += b is a shorthand for a = a + b .

How do I concatenate in NSString?

To concatenate two strings into a new string: NSString *string1 = @"This is"; NSString *string2 = @" a test.

How do I combine two string values?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


1 Answers

Two things I noted:

The use of NSMutableString is not needed. Just write for the first case

cell.textLabel.text = [appDelegate.indexArray objectAtIndex:indexPath.section];

For the 2 other cases you don't use the string at all (or it's not shown in your code).

In the for-loops, you're always overwriting aString and aStringtitle, and that even with a new alloc. Appending goes like this:

NSString *aString = @"";
for ...
  aString = [aString stringByAppendingString:[appDelegate.indexArray objectAtIndex:i]];

or

    aString = [aString stringByAppendingFormat:@" %@", [appDelegate.indexArray objectAtIndex:i]];

Check the NSString Class Reference for details.

like image 104
ott-- Avatar answered Nov 15 '22 17:11

ott--