Hi I'm generating a string which consists of text entries from text fields.
NSString * lines = [NSString stringWithFormat:@"%@,%@,%@,%@,%@", [self.crabText text], [self.trawlText text], [self.trapText text], [self.vesselText text], [self.lengthText text]];
I generate the above string when the user selects a "Generate CSV" UIButton which follows by an IBAction. However, I then want to clear the text fields and append the new populated entries on top of the previous ones. However, only the current text field entries will be populated due to the function always creating a new instance of the "lines" string.
lines = [NSString stringWithFormat: @"%@\n", lines];
I have this working by putting all new strings into an array but was wondering if it were possible to just use the same string. An example would be "1,1,1,1,1" "new line" "2,2,2,2,2".
Don't do this:
NSString * lines = [NSString stringWithFormat:@"%@,%@,%@,%@,%@", [self.crabText text], [self.trawlText text], [self.trapText text], [self.vesselText text], [self.lengthText text]];
Do this:
NSArray *fields = @[[self.crabText text],
[self.trawlText text],
[self.trapText text],
[self.vesselText text],
[self.lengthText text]];
NSString *line = [fields componentsJoinedByString:@","];
Then, to join two lines together:
NSArray *lines = @[lineOne, lineTwo];
NSString *linesString = [lines componentsJoinedByString:@"\n"];
Yes, you could use an NSMutableString and appendString to append to it each time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With