How to get last array values from NsmutableArray [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the last object of an NSArray
In my iPhone application, I want to retrieve the last array value from NSMutableArray:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:messages];
NSLog(#"array : %#", array);
In this "array". I having this below values.
hi, hello,
I want to take hello values only from the NSMutableArray.
How can I do this?

Try this
NSLog(#"array : %#",[array lastObject]);
It may help's you

you should follow indexpath at [array count] - 1 to obtain the last value.
For Ex,
NSString *str = [NSString stringWithFormat:"%#",[array objectAtIndex:[array count] - 1]];
NSLog("last object of an array = %#",str);
Enjoy Programming!

You can use:
NSString *str = (NSString *)[array lastObject];

[array lastObject] returns the last object of the array.
NSArray *arr=[NSArray arrayWithObjects:#"A",#"B",#"C", nil];
NSString *last=[arr lastObject];
NSLog(#"Last obj in string : %#",last);
Output :
Last obj in string : C

Related

removeobject from nsmutablearray

I m doing a quiz application in which I have stored the correctanswer and its question Id in a NSMutableArray.I m displaying it in a section tableView.Now if I mark another answer in the same section then the prevoius have to be removed and this one has to be attached to the array.
But Im unable to get it .
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
int n=[cellSection count];
NSString *questionId=[[cellSection objectAtIndex:n-1]objectForKey:#"QId"];
for (NSDictionary *dict in selectedOptionandQIdArray) {
NSString *str=[dict valueForKey:#"QId"];
[arr1 addObject:str];
}
BOOL isTheObjectThere = [arr1 containsObject:questionId];
if(isTheObjectThere==YES)
{
//remove the prevoius answer and replace by the new answer for the same question id
}
[selectedOptionsArray addObject:dictionary1];
NSDictionary *dictinary2 =[[NSDictionary alloc] initWithObjectsAndKeys:questionId, #"QId", nil];
[selectedOptionsArray addObject:dictinary2];
[selectedOptionandQIdArray addObject:selectedOptionsArray];
But isTheObjectThere is always 'NO' eventhough there is same question Id.
How can I remove prevoius one and replace by the new one
id object = [[array objectAtIndex:0] retain];
[array removeObjectAtIndex:0];
[array insertObject:object atIndex:2];
Use these lines.
use replaceObjectAtIndex:withObject:. just replace your answer.like this
[selectedOptionsArray replaceObjectAtIndex:indexofrepacingElement withObject:#"newAnswer"];

Read from txt list file and set many objects in ios

I, I'm writing an application that has to read the content of txt.
this txt is such a property file with a list formatted in this way:
1|Chapter 1|30
2|Chapter AA|7
3|Story of the United States|13
........
keys are separated by "|".
I googled a lot hoping to find any "pragmatically solution" but nothing...
how can I read these informations and set many objects like:
for NSInterger *nChapter = the first element
for NSString *title = the second element
for NSInteger *nOfPages = the last element ?
NSString's - (NSArray *)componentsSeparatedByString:(NSString *)separator could be your best friend.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-componentsSeparatedByString_
Only if you read NSString's class reference:
NSString *str = [NSString stringWithContentsOfFile:#"file.txt"];
NSArray *rows = [str componentsSeparatedByString:#"\n"];
for (NSString *row in rows)
{
NSArray *fields = [row componentsSeparatedByString:#"|"];
NSInteger nChapter = [[fields objectAtIndex:0] intValue];
NSString *title = [fields objectAtIndex:1];
// process them in here
}

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

How to sort NSArray? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to sort a NSArray alphabetically?
I am using below code to load data into UITableView from NSArray.
How can I sort them in alphabetical order ?
Mine is NSArray not NSMutableArray nor I am using Dictionary
defaultVot = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathToVot error:&error];
NSString *fileName = [[defaultVot objectAtIndex:indexPath.row-1] lastPathComponent];
NSString *filenameWithoutExtension = [fileName substringToIndex:[fileName length] -4];
cell.textLabel.text = filenameWithoutExtension;
You should probably sort your array earlier. (Likely when you load the list.) Then use the sorted array as the datasource.
NSArray * defaultVot = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathToVot error:&error];
NSMutableArray * array = [NSMutableArray arrayWithCapacity:[defaultVot count]];
for (NSString * f in defaultVot){
NSString *fileName = [f lastPathComponent];
NSString *filenameWithoutExtension = [fileName substringToIndex:[fileName length] -4];
[array addObject:filenameWithoutExtension];
}
NSArray * sortedArray = [array sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
sortedArray = [yourArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
Check this
And also this
You can see Apple's documentation on how to accomplish this.
As an example:
// From the Documentation
sortedArray =
[anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
This will return a new array that is sorted using the rules for the current locale (in a case insensitive manner).

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.