简体   繁体   English

在objc中动态添加字符串

[英]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. 我想从result.text动态添加字符串值,并希望以这种方式显示它[@"17052648287",@"17052607335"]而不会丢失该值。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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