本文介绍了NSMutableArray到std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将NSMutableArray的内容转换为std :: vector?如果是这样,应该在Objective-C还是C ++代码中完成?

Is it possible to convert the contents of an NSMutableArray to a std::vector? And if so, should this be done in the Objective-C or C++ code?

推荐答案

您可以创建具有任何Objective-C类型的向量.
例如,要将NSString实例存储到向量中,可以使用下一个代码:

You can create a vector with any Objective-C type.
For example to store a NSString instance into a vector, you can use next code:

    NSMutableArray<NSString*>* array = [@[@"1", @"2"] mutableCopy];

    __block std::vector<NSString*> vectorList;
    vectorList.reserve([array count]);
    [array enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        vectorList.push_back(obj);
    }];

    for (std::vector<NSString*>::const_iterator iterator = vectorList.cbegin(); iterator != vectorList.cend(); iterator++)
    {
        NSLog(@"%@", *iterator);
    }

您应该在Objective-C ++文件中使用它,因为C ++没有Objective-C的语法(具有mm扩展名的文件).

You should use it in Objective-C++ files, because C++ does not have syntax for Objective-C (files with mm extension).

如果需要将NSMutableArray中的数据转换为其他表示形式,例如,将NSNumber转换为int或将NSString转换为std :: string,则应手动创建它.

If you need to convert a data inside NSMutableArray to different representation, for example, NSNumber to int or NSString to std::string, you should create it by hand.

这篇关于NSMutableArray到std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 19:41