目标-C:合并多个字符串数组 [英] Objective - C: Combine multiple string arrays

查看:74
本文介绍了目标-C:合并多个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个NSString数组,我想根据用户的偏好组合成一个数组.

I have multiple NSString Arrays that I would like to combine into a single array based on user preferences.

创建数组:

static const NSString *string1[] = 
{...};

static const NSString *string2[] =
{...};

static NSMutableString *string3[] =
{

};

String3是保留数组,其中添加了所有用户的选择.有8个不同的字符串可以打开或关闭,因此有很多可能的组合.我已经尝试了很多方法,但都没有成功.例如:

String3 is the holding array where all of the user's choices are added. There are 8 different strings that could be toggled on or off, so a fair number of possible combinations. I've tried a number of things with no success. For example:

*string3=[string3 arrayByAddingObjectsInArray:string 2];

给出警告:

实例方法'-arrayByAddingObjectsInArry:'未找到(返回类型默认为'id')

接收器类型'NSMutableString **'不是'id'或接口指针,请考虑将其强制转换为'id'

感谢您的帮助.

推荐答案

您的基本问题是,您混淆了两种称为数组"的东西.您所拥有的是C数组-它们不是对象,因此您无法向它们发送消息(例如 arrayByAddingObjectsInArray:).您想要是一个NSArray.

Your basic problem is that you're confusing two different things called "arrays." What you have there are C arrays — they're not objects, so you can't send messages (such as arrayByAddingObjectsInArray:) to them. What you want is an NSArray.

将它们全部声明为 NSArray * strings1,* strings2,* strings3 ,然后编写一些初始化它们的方法,如下所示:

Declare them all as NSArray *strings1, *strings2, *strings3, and then write some method to initialize them like so:

+ (id)createArrays {
    strings1 = [[NSArray alloc] initWithObjects:@"Something", @"Something else", nil];
    strings2 = [[NSArray alloc] initWithObjects:@"Yet another thing", nil];
    strings3 = [[strings1 arrayByAddingObjectsFromArray:strings2] retain];
}

您将要确保在此处正确管理内存,否则您会疯狂地泄漏.通常,让对象属于某个类更好,因此您可以使用setter而不是为您管理内存,而不是将它们存储在全局或静态变量中.

You'll want to make sure you manage your memory correctly here or you'll leak like crazy. It's usually better to have objects belong to some class, so you can use setters than manage memory for you, rather than store them in global or static variables.

这篇关于目标-C:合并多个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆