简体   繁体   中英

add string dynamically in objc

I want to add string value dynamically from result.text and I wanted to display it in this way [@"17052648287",@"17052607335"] without losing the value. How can I do it?

NSMutableArray *strings = [@[@"17052648287",@"17052607335"] mutableCopy];

Add on coding

- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result{
    if (!result) return;

if(self.hasScannedResult == NO)
{
    //Scan Result, added into array
    NSString *scanPackage = [NSString stringWithFormat:@"%@", result.text];

    scanLists = [NSMutableArray new];
    [scanLists addObject:scanPackage];

    NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
    NSMutableArray *strings = [[NSMutableArray alloc]init];
    strings = [@[result.text] mutableCopy];
    [preferences setObject:strings forKey:@"strings"];
    NSMutableArray *stringsArray = [preferences objectForKey:@"strings"];
    for (NSString *string in stringsArray) {
        NSLog(@"string: %@", string);
    }

Declare an results array:

NSMutableArray * array = [NSMutableArray new];

Write below code where you get your result.text:

NSString *scanPackage = [NSString stringWithFormat:@"%@", result.text]; // If this code is working for you
[array addObject: scanPackage];
NSString *combined = [array componentsJoinedByString:@","];
NSLog(@"combined: %@", combined);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM