Adding string Elements in NSArray - iphone

I have mutable string array named arrayout.
It is having 3 element .Now I want to add 1 String element that array.But when I try to add,it is taking null value....Cant get what to do...Please help...
My code is :
NSString *ds1 = #"--";
[arrayout arrayByAddingObject:ds1];
NSLog(#"arrrrr '%#'",arrayout);

Try this out:
NSString *ds1 = #"--";
[arrayout addObject:ds1];
NSLog(#"arrrrr '%#'",arrayout);
Hope this helps you.

You can also do it this way:
NSMutableArray *arrayout = [[NSMutableArray alloc] init]; // alloc here
[arrayout insertObject:#"SomeText Here" atIndex:[arrayout count]]; // insert here
NSLog(#"Appended Array: '%#'",arrayout); // Print here
this will populate arrayout with SomeText Here.
Hope it helps!

Why are you concatenating strings like this ? You can just do something simple like
NSString* newString = [NSString stringWithFormat:#"%#/%#/%#", string1, string2, string3];

Related

Split String into Array Objects

I am getting response from web-service like this
Result = (
(
"Test1",
"Test2",
"Test3",
"Test4",
Test5
)
);
By, [dict objectForKey:#"Result"]; I am getting value
(
"Test1",
"Test2",
"Test3",
"Test4",
Test5
)
So, how can I get values in array format from NSSting format.
For eg. Array :- Test1, Test2, Test3, Test4, Test5
Can anyone reply please.
Thanks in advance.
try This Code :
NSMutableArray* arrCountryName = (NSMutableArray *)[yourString componentsSeparatedByString: #","];
If you copied the printout exactly as formatted, it appears that the #"Result" key contains an array of arrays with a single element which is already the array that you wanr, so this should work for you:
NSArray *res = [[dict objectForKey:#"Result"] objectAtIndex:0];
The reason I think this is an array in an array is that it shows two levels of nested parentheses around the list.
Here you can use your inputString and generate the Array output as follows:
NSString *inputString = #"Test1,Test2,Test3,Test4";
NSMutableArray* outputArray = [[NSMutableArray alloc] init];
outputArray = [inputString componentsSeparatedByString: #","];
If you need any more help them let me know.
try this:
if([dict objectForKey:#"Result"] isKindofClass:[NSArray class]])
{
NSArray *arrValues = [dict objectForKey:#"Result"];
//u have your array
for(NSString *value in arrValues)
{
NSLog(#"%#",values);
}
}
NSString *separatorString = #",";
NSScanner *aScanner = [NSScanner scannerWithString:yourString];
NSString *thingsScanned;
[aScanner scanUpToString:separatorString intoString:&thingsScanned];
NSLog(#"container: %#", thingsScanned);

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

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

How to create image and labels using location data stored in NSArray

i have to create an 2images and 3 labels by using code (cgrectmake)and i am having X location, y location, width and height all are stored in arrays(which i have retrieved from the web services)how can i create the image and labels can any one help me
You can join the elements of an array together with the NSString componentsJoinedByString class method:
NSString myString = [myNSArray componentsJoinedByString:#"x"];
where x is the characters you'd like to appear between each array element.
Edited to add
So in your newly-added code if these are the label values:
lbl = #"zero"
lbl1 = #"one"
lbl2 = #"two"
and you want to join them together with a space character then if you did this:
NSString *temp = [labelArray componentsJoinedByString:#" "];
NSLog(#"temp = %#", temp);
then this is what would be logged:
zero one two
Edited to further add
If you are instead trying to join the label values together to make xml elements then you might do something like this:
NSString *joinedElements = [labelArray componentsJoinedByString:#"</label><label>"];
NSString *temp = [NSString stringWithFormat:#"<label>%#</label>", joinedElements];
NSLog(#"temp = %#", temp);
then this is what would be logged:
<label>zero</label><label>one</label><label>two</label>
may be this is usefull to you.
NSString *str;
str = [arrayName objectAtIndex:i(Index NO)];
OK by this easily you can access object from the array. any type of object u can fetch this way only reception object type are change in left side.
Best of Luck.
Most objects have a -description method which returns a string representation of the object:
- (NSString *)description;
For example:
NSArray *array = [NSArray arrayWithObjects:#"The", #"quick", #"brown", #"fox", nil];
NSLog(#"%#", array); // prints the contents of the array out to the console.
NSString *arrayDescription = [array description]; // a string
It would help to know what you want to do with the string (how will you use the string). Also, what kind of objects do you have in the array?
In that case, Matthew's answer is one possibility. Another might be to use an NSMutableString and append the individual items, if you need control over how the string is created:
NSMutableString *string = [NSMutableString string];
if ([array count] >= 3) {
[string appendString:[array objectAtIndex:0]];
[string appendFormat:#"blah some filler text %#", [array objectAtIndex:1]];
[string appendString:[array objectAtIndex:2]];
}

assign value from array to string

i have an array of 5 objects.
i want to assign object which is at index 1, to an NSSTRING.
nsstring *abc = [array objectAtindex:1];
i know this is wrong syntax, this is returning object , something like this.
how can i get value which is at index 1 and assign it to an string?
regards
Erm.. this is the correct syntax :)
Apart the name of the string class:
NSString *abc = [array objectAtIndex:1];
mind that this won't create a copy of the string, if you need to copy it use
NSString *abc = [NSString stringWithString:[array objectAtIndex:1]];
As Eiko notes you can directly copy the string object if you need to:
NSString *abc = [[array objectAtIndex:1] copy];
Arrays are zero based in Objective-C land. So...
NSArray *array = [NSArray arrayWithObjects:#"one", #"two", nil];
NSString *abc = [array objectAtIndex:1];
Would return the second object in the array. Zero would return the first.