assign value from array to string - iphone

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.

Related

objective c getting substring after and before dot

i have a string like 112.1 or 102.2 etc now i want to get the substring before dot and after the dot using objective c, means i want to get 2 substrings like this 112 and 1. please guide. Regards Saad.
Try the following:
NSArray *arrayWithTwoStrings = [yourString componentsSeparatedByString:#"."];
Using this method you'll get NSArray containing string components that were separated by "." string, that is "112" and "1" for "112.1" string. After that you can access them using array's objectAtIndex: method.
P.S. Note also that if you're going to use numeric values of those strings there might be better solution
NSString *str = yourstr;
NSArray *Array = [str componentsSeparatedByString:#"."];
NSString *t = [Array objectAtIndex:0];
NSString *t1 = [Array objectAtindex:1];
NSArray* components = [yourString componentsSeparatedByString:#"."];

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

Split one string into different strings

i have the text in a string as shown below
011597464952,01521545545,454545474,454545444|Hello this is were the message is.
Basically i would like each of the numbers in different strings to the message eg
NSString *Number1 = 011597464952
NSString *Number2 = 01521545545
etc
etc
NSString *Message = Hello this is were the message is.
i would like to have that split out from one string that contains it all
I would use -[NSString componentsSeparatedByString]:
NSString *str = #"011597464952,01521545545,454545474,454545444|Hello this is were the message is.";
NSArray *firstSplit = [str componentsSeparatedByString:#"|"];
NSAssert(firstSplit.count == 2, #"Oops! Parsed string had more than one |, no message or no numbers.");
NSString *msg = [firstSplit lastObject];
NSArray *numbers = [[firstSplit objectAtIndex:0] componentsSepratedByString:#","];
// print out the numbers (as strings)
for(NSString *currentNumberString in numbers) {
NSLog(#"Number: %#", currentNumberString);
}
Look at NSString componentsSeparatedByString or one of the similar APIs.
If this is a known fixed set of results, you can then take the resulting array and use it something like:
NSString *number1 = [array objectAtIndex:0];
NSString *number2 = [array objectAtIndex:1];
...
If it is variable, look at the NSArray APIs and the objectEnumerator option.
NSMutableArray *strings = [[#"011597464952,01521545545,454545474,454545444|Hello this is were the message is." componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#",|"]] mutableCopy];
NString *message = [[strings lastObject] copy];
[strings removeLastObject];
// strings now contains just the number strings
// do what you need to do strings and message
....
[strings release];
[message release];
does objective-c have strtok()?
The strtok function splits a string into substrings based on a set of delimiters.
Each subsequent call gives the next substring.
substr = strtok(original, ",|");
while (substr!=NULL)
{
output[i++]=substr;
substr=strtok(NULL, ",|")
}
Here's a handy function I use:
///Return an ARRAY containing the exploded chunk of strings
///#author: khayrattee
///#uri: http://7php.com
+(NSArray*)explodeString:(NSString*)stringToBeExploded WithDelimiter:(NSString*)delimiter
{
return [stringToBeExploded componentsSeparatedByString: delimiter];
}

how to concatenate values to the string

i want to cancatenate strings with comma as a separator and result must be stored in string...
comma=#",";
for(i=0;i<[resources count];i++)
{
Record *aRecord = [resources objectAtIndex:i];
temp=aRecord.programID;
if(i==0)
pid=temp;
else
//i am using this one to cancatenate but is not working why?
pid = [NSString stringWithFormat:#"%#%#%#", pid,comma,temp];
}
Use the -componentsJoinedByString: method on NSArray:
NSArray *csvArray = [NSArray arrayWithObjects:#"here", #"be", #"dragons", nil];
NSLog(#"%#", [csvArray componentsJoinedByString:#", "]);
(from the docs)
Cast the id types to NSString and then use the concatenation methods found in the class reference of NSString.