How can i add three objects into one in mutableArray in iphone application? - iphone

NSMutableArray * mutableArra = [[NSMutableArray alloc] init];
[mutableArra addObjectsFromArray:myArray];
NSLog(#"=====mutable aarrya ====::%#",mutableArra);
This is the result from the previous code:
01,12,2012
How can I add these three objects into one like the following?
01-12-2012
Thank you in advance.

I'm not sure how do you want to "add" those poor objects to one another, but if you want them to be concatenated as a string, try:
NSString *result = [myArray componentsJoinedByString:#"-"];

If this is for dates, I would recommend looking into NSDate which will do the formatting for you.

Related

Adding dictionaries to another

a silly question:
How do i fit multiple dictionaries together in one or add one to an other?
i tried to initialize my dictionary with two other.. it didn't work.
dic1 = [[NSMutableDictionary alloc] initWithDictionary:dic2, dic3];
thank you
You want to use the addEntriesFromDictionary:dic3 method on NSMutableDictionary:
dic1 = [[NSMutableDictionary alloc] initWithDictionary:dic2];
[dic1 addEntriesFromDictionary:dic3];

How to declare 2D array in Objective-C?

I am new to iPhone. I tried a lot to declare 2D array but I am unable to get the result.
NSMutableArray *outerarray = [[NSMutableArray alloc] init];
outerarray = [NSMutableArray arrayWithObjects:
[NSMutableArray arrayWithObjects:#"10-20","21-30","31-40","41-50","51-60","61-70","71-80","81-90","91-100",nil,
[NSMutableArray arrayWithObjects:#"10-20","21-30","31-40","41-50","51-60","61-70","71-80","81-90","91-100",nil],
nil];
I want to combine two arrays elements and compare it with other element.
Anyone can help me please?
#"..." is an NSString. "..." is not, it's a C string constant. You need to use the #"..." syntax for all NSString objects you're putting into an NSArray.
NS[Mutable]Array does not support 2D as far as I know. You can probably jury-rig it to mutate and access elements in 2D (make that its own class). Once you can access arbitrary elements, you can "combine" them and compare them as you need. The alternative is since Objective-C is a superset of C, its int arrayName [][] is available (but discouraged).

Adding string to an array

i wanted to know how to add strings into an array.
I used the following methods but it is showing null.
1) [arrData addObject:[NSString stringWithString:strlast]];
2) [arrData addObject:strlast];
Thanks in advance
You can't add anything to an NSArray once it's created. You need to use an NSMutableArray if you want to make changes to it.
Update: You may actually have two problems.
Using an NSArray instead of an NSMutableArray when mutability is needed.
Not initializing the array object (either kind). If arrData is nil, you can happily send as many messages as you want to nil. Nothing will happen.
If it is showing null (nil) you need to make sure you set arrData somewhere in your code before trying to addObject:.
arrData = [[NSMutableArray alloc] init];
Also strlast is a string so use your second example, the first example is pointless.
[arrData addObject:strlast];
Did you allocate an array and assign it to arrData?
Try:
NSMutableArray *arrData = [NSMutableArray array];
NSString *string = #"My string";
[arrData addObject:string];
NSLog(#"%#", [arrData objectAtIndex:0]); //logs "My string"
If you're using a non-mutable array, you can also use arrayByAddingObject:
arrData = [arrData arrayByAddingObject: strlast];
but a mutable array is probably a better idea.

addObject to NSMutableArray not working for iPhone App

There have been a few threads on this topic but none have been able to solve my problem. Essentially I am trying to add a custom object to an NSMutableArray and it doesn't seem to be adding. I don't get any errors but I get a warning saying that my array is an "unused variable" so it looks like it is not getting used. See code below. Any help is appreciated!
Here is the initialization in the app delegate (on run time it says this array is not being used):
NSMutableArray *organArray = [[NSMutableArray alloc] init];
Here is my object class organ.m (I am importing the app delegate, the rootviewcontroller and the organ.h file)
Organ *organObj = [[Organ alloc] initWithPrimaryKey:primaryKey];
organObj.organName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
organObj.isDirty = NO;
[appDelegate.organArray addObject: organObj];
[organObj release];
I know the organObj.organName is getting the correct values from my sqlite db because I can output them to the console. They just don't seem to be getting added to the array and the fact that it says the array is not being used means something is wrong.
Thanks in advance
Just a guess but if organArray is intended to be a member of your app delegate, you are creating a new organArray when prefixing it with "NSMutableArray" so if I understand your code, change your app delegate to:
organArray = [[NSMutableArray alloc] init];
instead of:
NSMutableArray *organArray = [[NSMutableArray alloc] init];

iPhone - Merge keys in a NSArray (Objective-C)

I'm having troubles with arrays and keys... I have an array from my database:
NSArray *elementArray = [[[menuArray valueForKey:#"meals"] valueForKey:#"recipe"] valueForKey:#"elements"]
The problem here is that I would like all my elements of all my meals of all my menus in an array such that:
[elementArray objectAtIndex:0] = my first element
etc...
In the example above, the elements are separated by the keys.
How can I get that?
Hope it's clear enough...
Thanks
From your code snippet, it is not clear to me exactly how your data is structured, but I think it's analogous to having an NSDictionary (called aDictionary) of NSArray and wanting to combine all the NSArray into one. If this is the case, then:
NSMutableArray *resultArray = [[[NSMutableArray alloc] init] autorelease];
for (id dictionaryKey in aDictionary) {
[resultArray addObjectsFromArray:[aDictionary objectForKey:dictionaryKey]];
}
return [NSArray arrayWithArray:resultArray];
(This code has not been tested.)