x19
x19

Reputation: 13

Objective - C: Combine multiple string arrays

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

The arrays are created:

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

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

static NSMutableString *string3[] =
{

};

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];

That gives warnings:

Instance method '-arrayByAddingObjectsInArry:' not found (return type defaults to 'id')

and

Receiver type 'NSMutableString **' is not 'id' or interface pointer, consider casting it to 'id'

Thanks for your help.

Upvotes: 0

Views: 3582

Answers (4)

Jonathan Naguin
Jonathan Naguin

Reputation: 14766

You are using C array that holds NSString objects. The idea is:

*string3 = malloc(sizeof(NSString*) * count); // Array of count strings
string3[0] = string1[0]; // Put on at index 0
NSLog(@"%@", string1[0]); // Log string at index 0

"count" is the sum of "string1" plus "string2".

You can do a for loop with this.

Upvotes: 0

Chuck
Chuck

Reputation: 237010

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.

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];
}

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.

Upvotes: 1

ColdLogic
ColdLogic

Reputation: 7265

Ummmmm try this code instead

NSArray *array1 = [NSArray arrayWithObjects:@"Wow", @" that", nil];
NSArray *array2 = [NSArray arrayWithObjects:@" is", @" really", nil];
NSArray *array3 = [NSArray arrayWithObjects:@" terrible", @" code", nil];

NSArray *fullArray = [array1 arrayByAddingObjectsInArray:[array2 arrayByAddingObjectsInArray:array3]];

NSLog(@"%@", fullArray);

The problem you have above is because you are trying to use arrayByAddingObjectsInArray: on a class that doesn't have that method defined. NSArray is the class with that method, so you need an instance of NSArray to use arrayByAddingObjectsInArray like the code above.

Upvotes: 1

zaph
zaph

Reputation: 112857

NSStringis not an array, there is a terminology issue.

NSStrings are objects so to create a NSString from static text:

NSString *string1 = @"the text";

To combine strings use a method found under NSString.

To add a string to string 3 (which is an NSMutableString):

[string3 appendString: string2];

"Instance method -arrayByAddingObjectsInArry: not found" means that there is no method named -arrayByAddingObjectsInArry: as part of the NSString class since that is the class instance you are sending the message to. (Yes there is a type but that is not the problem).

Consider browsing the NSString documentation in the Xcode Organizer window Documentation tab.

Upvotes: 0

Related Questions