在objc中动态添加字符串

时间:2017-06-07 04:12:52

标签: ios objective-c iphone xcode

我想从result.text动态添加字符串值,我希望以这种方式[@"17052648287",@"17052607335"]显示它而不会丢失值。我该怎么做?

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

添加编码

- (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);
    }

1 个答案:

答案 0 :(得分:0)

声明结果数组:

NSMutableArray * array = [NSMutableArray new];

在下面的代码中写下你的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);