Joining all the elements in an NSMutableArray to form an NSString - iphone

I have an NSMutableArray of NSString objects. How would I make an NSString that consists of all the elements in that array joined together with semicolons in between?
For example:
array = "hello","hi","bye",nil;
output = "hello;hi;bye";

NSMutableArray *array = [NSMutableArray arrayWithObjects:#"hello", #"hi", #"bye", nil];
NSString *output = [array componentsJoinedByString:#";"];

Related

How do I move all the data from NSMutableArray to NSArray with a key

I have an array NSMutableArray. It contained objects from the XML file - #"time". I need to move all records from NSMutableArray with key #"time" to NSArray. This is in order that would be based on the date on the calendar is highlighted markers. How can I implement it?
If I write the code:
NSDictionary *nItem = [rasp objectAtIndex:0]; //here instead of 0, you should put the number of elements in the array, but I do not get ((
NSArray *data = [NSArray arrayWithObjects:[nItem objectForKey:#"time"], nil];
NSMutableArray is allocated only a first date from the array, index 0.
You could do something like this.
NSMutableArray* mutable = [NSMutableArray arrayWithCapacity:0];
for(NSDictionary* nItem in rasp)
{
[mutable addObject:[nItem objectForKey:#"time"]];
}
NSArray *data = [NSArray arrayWithArray:mutable];
Hope it helps!

Retrieve the substring of a strings which are in array

I am new to iphone.I have an array which contains the objects like below
"04_Num/04Num001.mp3",
"04_Num/04Num002.mp3",
"04_Num/04Num003.mp3",
"04_Num/04Num004.mp3",
"04_Num/04Num005.mp3",
"04_Num/04Num006.mp3",
"04_Num/04Num007.mp3",
"04_Num/04Num008.mp3",
"04_Num/04Num009.mp3",
"04_Num/04Num010.mp3",
"04_Num/04Num011.mp3",
"04_Num/04Num012.mp3",
"04_Num/04Num013.mp3",
"04_Num/04Num014.mp3",
"04_Num/04Num015.mp3",
"04_Num/04Num016.mp3",
"04_Num/04Num017.mp3",
"04_Num/04Num018.mp3",
"04_Num/04Num019.mp3",
"04_Num/04Num020.mp3",
"04_Num/04Num021.mp3",
"04_Num/04Num022.mp3",
"04_Num/04Num023.mp3",
"04_Num/04Num024.mp3",
"04_Num/04Num025.mp3",
"04_Num/04Num026.mp3",
"04_Num/04Num027.mp3",
"04_Num/04Num028.mp3",
"04_Num/04Num029.mp3",
"04_Num/04Num030.mp3",
"04_Num/04Num031.mp3",
"04_Num/04Num032.mp3",
"04_Num/04Num033.mp3",
"04_Num/04Num034.mp3",
"04_Num/04Num035.mp3",
"04_Num/04Num036.mp3"
but here i want retrieve the strings(objects)only after the / (i.e) for example 04_Num/04Num033.mp3 in this i want only the string 04Num033.mp3.Like this for all the above and then i have to place in an array
how it is possible if any body know this please help me...
lastPathComponent is what you need. You could do it like so:
NSMutableArray *files = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *file in songs) //Where songs is the array with the paths you have provided
{
[files addObject:[file lastPathComponent]];
}
You can separate the string into two parts using NSString's
componentsSeparatedByString:
method, and use the last string component
// Let's call your array of strings as stringsArray
NSMutableArray *prefixStrings = [[NSMutableArray alloc] init];
for (NSString *str in stringsArray) {
NSArray *stringComponents = [str componentsSeparatedByString:#"/"];
if ([stringComponents count]) {
[prefixStrings addObject:[stringComponents objectAtIndex:1]];
} }

add data to NSMUtableArrray with keys by for loop

I'm new in iPhone, I want to add elements to NSMutableArray with each element's name
I created a MutableArray for keys , then other array for elements that I get them from object called Pages.
I wrote the following code
NSMutableArray *myArray;
NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:#"b_pag_id", #"b_pag_bo_id", #"b_pag_num", #"b_pag_note", #"b_page_mark", #"b_page_stop", #"b_pag_user_id", nil];
for (int x=0; x<[pages count]; x++) {
Pages *myPages = (Pages *)[self.pages objectAtIndex:x];
NSString *b_pag_id2 = [NSString stringWithFormat:#"%d",myPages.b_pag_id];
NSString *b_pag_bo_id2 = [NSString stringWithFormat:#"%d",myPages.b_pag_bo_id];
NSString *b_pag_num2 = [NSString stringWithFormat:#"%d",myPages.b_pag_num];
NSString *b_pag_note2 = myPages.b_pag_note;
NSString *b_page_mark2 = [NSString stringWithFormat:#"%d",myPages.b_page_mark];
NSString *b_page_stop2 = [NSString stringWithFormat:#"%d",myPages.b_page_stop];
NSString *b_pag_user_id2 = [NSString stringWithFormat:#"%d",myPages.b_pag_user_id];
NSMutableArray *arrayValue = [[NSMutableArray alloc] initWithObjects:b_pag_id2, b_pag_bo_id2, b_pag_num2, b_pag_note2, b_page_mark2, b_page_stop2, b_pag_user_id2, nil];
NSDictionary *theReqDictionary = [NSDictionary dictionaryWithObjects:arrayValue forKeys:arrayKey];
myArray = [NSMutableArray arrayWithObjects:theReqDictionary,nil];
}
NSLog(#"array size: %d", [myArray count]);
I want to add every element to its key for example
element (b_pag_id2) its key (b_pag_id) ..etc
is this right ?? or how to do this ??
consider that NSLog(#"array size: %d", [myArray count]); gives me 1 and the size of my elements is 14
Before the loop you need to initialize the aray
NSMutableArray *myArray = [NSMutableArray array];
Inside the loop replace following:
myArray = [NSMutableArray arrayWithObjects:theReqDictionary,nil];
with
[myArray addObject:theReqDictionary];
The problem is that you are creating a new array with 1 dictionary in every loop iteration. Instead you need to initialize the array and add values one by one.
Each time through your loop you are creating a new array for myArray that has only one element. You should initialize an empty NSMutableArray before the loop and then simply add your new object to it instead of using arrayWithObjects: to create myArray..
Here i'm giving a short example, and i hope this will help you.
see this code :-
NSMutableArray *arrayValue = [[NSMutableArray alloc]initWithObjects:#"Value1",#"Value2",#"Value3", nil];
NSMutableArray *arrayKey = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
for(int i=0;i<3;i++)
{
[dic setObject:[arrayValue objectAtIndex:i] forKey:[arrayKey objectAtIndex:i]];
}
//and you can see this by printing it using nslog-
NSLog(#"%#",[dic valueForKey:#"1"]);
Thank you!!!

Is it possible to get an array to show up as text?

I'm trying to do something like this ..
NSString *string = [NSString stringWithFormat:#"Hello World"];
NSArray *array = [NSArray arrayWithObject:string];
[uitextviewOutlet setText:[NSArray arrayWithArray:array]];
and I'd like for that to show up on my uitextviewOutlet window, which is an object of UITextView that will print out text.
The code works if I straight out send the uitextviewOutlet object the setText message and if it takes string as the parameter, but it won't take the array.
is there a way to have it take an array?
TIA.
You can join the elements with, let's say a comma like this: NSString *joinedString = [array1 componentsJoinedByString:#","];
Edit_: I'm not a friend of "Do it for me", but here you go:
NSString *string = [NSString stringWithFormat:#"Hello World"];
NSArray *array = [NSArray arrayWithObject:string];
[uitextviewOutlet setText:[array componentsJoinedByString:#","]];
By the way, your code makes no sense, or do you fill up the array with more than just one value?
You can convert an array to a string with -componentsJoinedByString: as in #BjörnKaiser`s example. Or for more flexibility you can do:
NSString *string = [NSString stringWithFormat:#"Hello World"];
NSArray *array = [NSArray arrayWithObject:string];
for (NSString *araryItem in array) {
[uitextviewOutlet replaceRange:NSMakeRange(uitextviewOutlet.text.length, 0) withText:#"foo\n"];
[uitextviewOutlet replaceRange:NSMakeRange(uitextviewOutlet.text.length, 0) withText:arrayItem];
}

Array in UITextView

I have an arrayappdeligate.biblearray. I just want to display this array in a textview. This array contains sql datas of 4 types chapterno, verses, genisis and text. i need to extract only the verses and display it in textview how to do this?
It seems biblearray has the objects of type bible. You can get the verses from bible objects like this,
bible *_bible = (bible *)[appDelegate.bibleArray objectAtIndex:0];
textView.text = [_bible verses];
or directly as,
textView.text = [[appDelegate.bibleArray objectAtIndex:0] verses];
If you want to display all the verses in the textView, you can do it like this,
NSArray *allVerses = [appDelegate.bibleArray valueForKey:#"verses"];
textView.text = [allVerses componentsJoinedByString:#"\n\n"];
#"\n\n" adds two new lines between the verses.
You need to do some alteration. Create NSDisctionary instead.
Take a dictionary where you are adding data from database
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
NSMutableArray *chapterno = [[NSMutableArray alloc]init];
NSMutableArray *verses = [[NSMutableArray alloc]init];
NSMutableArray *genisis = [[NSMutableArray alloc]init];
NSMutableArray *text = [[NSMutableArray alloc]init];
//Add data you are getting from database
[chapterno addObject:chapternodata];
[verses addObject:versesdata];
[genisis addObject:genisisdata];
[text addObject:textdata];
[dict setValue:chapterno forKey:#"chapterno"];
[dict setValue:verses forKey:#"verses"];
[dict setValue:genisis forKey:#"genisis"];
[dict setValue:text forKey:#"text"];
[chapterno release];
[verses release];
[genisis release];
[text release];
Take one NSDictionary in AppDelegate say appDict and make it equal to dict
NSMutableArray *arrVerses = [[objAppDel.appDict objectForKey:#"verses"];
txt.text = [arrVerses description];