This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How is the code for separation of single array into two arrays?
hi all i am getting below response into single array when i used the JSON parsing from my required URL but here i have to divide single array response into three array sets one set will have TB3257,TB3259,TB3261,TB3263,TB3257,TB3260 and second array set will have TB3258,TB3260,TB3262,TB3259,TB3258 and TB3261 and third array set will have TB3258. so how is the code for dividing single array into three array sets in iphone?
(
" TB3257 TB3258",
" TB3259 TB3260",
" TB3261 TB3262",
" TB3263 TB3259",
" TB3257 TB3258 TB3258",
" TB3260 TB3261"
)
I just tried the following code:
NSArray *yourArray = [NSArray arrayWithObjects:#"TB3257 TB3258", #"TB3259 TB3260", #"TB3261 TB3262", #"TB3263 TB3259", #"TB3257 TB3258 TB3258", #"TB3260 TB3261", nil];
NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];
for (int i = 0; i < [yourArray count]; i++) {
NSArray *tempArray = [[yourArray objectAtIndex:i]componentsSeparatedByString:#" "];
[firstArray addObject:[tempArray objectAtIndex:0]];
[secondArray addObject:[tempArray objectAtIndex:1]];
if ([tempArray count] == 3) {
[thirdArray addObject:[tempArray objectAtIndex:2]];
}
}
NSLog(#"yourArray: %#\nfirst: %#\nsecond: %#\nthird: %#", yourArray, firstArray, secondArray, thirdArray);
Output was:
yourArray: (
"TB3257 TB3258",
"TB3259 TB3260",
"TB3261 TB3262",
"TB3263 TB3259",
"TB3257 TB3258 TB3258",
"TB3260 TB3261"
)
first: (
TB3257,
TB3259,
TB3261,
TB3263,
TB3257,
TB3260
)
second: (
TB3258,
TB3260,
TB3262,
TB3259,
TB3258,
TB3261
)
third: (
TB3258
)
Hope it helps
Related
This question already has answers here:
How do I sort an NSMutableArray with custom objects in it?
(27 answers)
Sorting two NSArrays together side by side
(4 answers)
Closed 9 years ago.
I have 2 NSMutableArrays. First array contains custom object intances with property NSString *itemID, second array contains only from NSString objects with same values of itemID, but in another order. I need to sort first array by itemID property of each object, and it should be sorted like second array.
How I can do this?
guideArray = < YOUR SECOND ARRAY WITH STRING OBJECT >;
unsortedArray = < YOUR FIRST ARRAY WITH CUSTOM OBJECT >;
[unsortedArray sortUsingComparator:^(id o1, id o2) {
Items *item1 = o1;
Items *item2 = o2;
NSInteger idx1 = [guideArray indexOfObject:item1.ItemID];
NSInteger idx2 = [guideArray indexOfObject:item2.ItemID];
return idx1 - idx2;
}];
NSLog(#"%#",unsortedArray);
Store the custom objects in an dictionary with itemID as key, use this dictionary as lookup to sort the objects:
NSArray *objects; // your objects
NSMutableArray *hintArray; // your sorted IDs
NSMutableDictionary *lookupDict = [[NSMutableDictionary alloc] initWithCapacity:[objects count]];
NSMutableArray *sortedObjects = [[NSMutableArray alloc] initWithCapacity:[hintArray count]];
for (id object in objects) {
[lookupDict setValue:object forKey:[object itemID]];
}
for (id hint in hintArray) {
[sortedObjects addObject:[lookupDict valueForKey:hint]];
}
EDIT:
Solution with inplace sort of objects:
NSMutableArray *objects;
NSMutableArray *hintArray;
NSMutableDictionary *lookupDict = [[NSMutableDictionary alloc] initWithCapacity:[hintArray count]];
int i = 0;
for (NSString *itemID in hintArray) {
[lookupDict setValue:[NSNumber numberWithInt:i] forKey:itemID];
i++;
}
[objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[lookupDict valueForKey:[obj1 itemID]] compare:[lookupDict valueForKey:[obj2 itemID]]];
}];
You can compare your two objects using following syntax :-
[items sortUsingComparator:^NSComparisonResult(Attribute *obj1, Attribute *obj2)
{
return [[NSNumber numberWithInt:[stringOrder indexOfObject:obj1.itemID]] compare:[NSNumber numberWithInt:[stringOrder indexOfObject:obj2.itemID]]]
}];
or else you can use following snippet :
NSArray* sortedKeys = [dict keysSortedByValueUsingComparator:^(id obj1, id obj2)
{
return [obj1 compareTo:obj2];
}
Enjoy Programming !
I've got a Plist file in my project that has a number of different arrays; each array is a different category which then contains additional arrays for individual questions. The problem I'm having is accessing the nodes and their values within these arrays.
Code I'm using:
-(IBAction)nextleft {
if (questionCounter > 1) {
questionCounter -= 1;
}
[self nextQuestion];
}
-(IBAction)nextright {
if (questionCounter < 20) {
questionCounter += 1;
}
[self nextQuestion];
}
-(void)nextQuestion {
NSArray *pair;
pair = [categories objectAtIndex:questionCounter];
plistQuestion = [pair objectAtIndex:0];
plistAnswer = [pair objectAtIndex:1];
abbreviation.text = plistQuestion;
}
My categories array is filled from my Plist file with this line;
categories = [NSArray arrayWithContentsOfFile:#"questions.plist"];
You can create a class in which you have two instances of NSString (question & answer) which consist the value of question & answer.Create an array of category which consist of object of that class.
Solution:
NSArray *categories = [NSArray arrayWithContentsOfFile:#"questions.plist"];
for (NSArray *pair in categories)
{
NSString *q = [pair objectAtIndex:0];
NSString *a = [pair objectAtIndex:1];
// process q and a
}
This question already has answers here:
How can I convert NSMutableArray into NSString?
(3 answers)
Closed 9 years ago.
hi all how to convert NSMutableArray values into the NSString
when i did as like
Webservices *details=[[Webservices alloc] init];
[details getMobileNumberDetails:phnotextfield.text];
NSLog(#"longitudes Arrays from %#",details.resultData);
"-91.57696007",
"10.343234",
"74.982343",
"76.464844",
"76.464844",
"2.256",
but when implemented like this
theCoordinate2.longitude = [[details.longarray objectAtIndex:0] floatValue];
NSLog(#"longitudes list from %#",theCoordinate2.longitude);
-91.57696007
but my intention is to assign all values to theCoordinate2.longitude so help me to store into string element in iphone.
You can use this code:
theCoordinate2.longitude = [[details.longarray objectAtIndex:0] stringByAppendingString:[details.longarray objectAtIndex:1];
//and so on for index 2,3,4 stringByAppendingString:[details.longarray objectAtIndex:2
NSLog(#"longitudes list from %f",theCoordinate2.longitude);
+1 to Wasif, but while he was composing his answer I was composing my own version of the code you could try:
NSMutableArray * longitudeArray = NULL;
NSString * detailsAsString = details.resultData;
if(detailsAsString)
{
NSArray * longitudeStringArray = [detailsAsString componentsSeparatedByString, #","];
if(longitudeStringArray && ([longitudeStringArray count] > 0))
{
longitudeArray = [[NSMutableArray alloc] initWithCapacity: [longitudeStringArray count]];
if(longitudeArray)
{
for(NSString * longitudeString in longitudeStringArray)
{
[longitudeArray addObject: [NSNumber numberWithFloat: [longitudeString floatValue]]];
}
}
} else {
NSLog( #"did not extract an array out of webservice data");
}
}
I have a table view, which has its data source from an array that contains names of people.
Now to make it easy to find people, I want to section the table view so that it has the letter A-Z on the right hand side, just like the Address Book app.
But my current array just contains a collection of NSStrings. How do I split them so that they are grouped by the first letter of the names? Is there any convenient way to do it?
EDIT: If anyone's interested in my final code:
NSMutableArray *arrayChars = [[NSMutableArray alloc] init];
for (char i = 'A'; i <= 'Z' ; i++) {
NSMutableDictionary *characterDict = [[NSMutableDictionary alloc]init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int k = 0; k < [myList count]; k++) {
NSString *currentName = [[friends objectAtIndex:k] objectForKey:#"name"];
char heading = [currentName characterAtIndex:0];
heading = toupper(heading);
if (heading == i) {
[tempArray addObject:[friends objectAtIndex:k]];
}
}
[characterDict setObject:tempArray forKey:#"rowValues"];
[characterDict setObject:[NSString stringWithFormat:#"%c",i] forKey:#"headerTitle"];
[arrayChars addObject:characterDict];
[characterDict release];
[tempArray release];
}
At the end of the function I'll have:
arrayChars [0] = dictionary(headerTitle = 'A', rowValues = {"adam", "alice", etc})
arrayChars[1] = dictionary(headerTitle = 'B', rowValues = {"Bob", etc})
Thank you everyone for your help!
You can use a dictionary to sort them, so create an array with all the letters you want to sort and a array with nil objects to initialize the dictionary
NSArray *names = #[#"javier",#"juan", #"pedro", #"juan", #"diego"];
NSArray *letters = #[#"j", #"p", #"d"];
NSMutableArray *objects = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [letters count]; ++i)
{
[objects addObject:[[NSMutableArray alloc] init]];
}
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:letters];
Then you must find the first letter if the word and put that word into the corresponding key in the dictionary
for (NSString *name in names) {
NSString *firstLetter = [name substringToIndex:1];
for (NSString *letter in letters) {
if ([firstLetter isEqualToString:letter]) {
NSMutableArray *currentObjects = [dictionary objectForKey:letter];
[currentObjects addObject:name];
}
}
}
To check you can print directly the dictionary
NSLog(#"%#", dictionary);
Then is your work to fill your sections in the tableview using the dictionary
i have an array like
[chapter,indent,left,indent,nonindent,chapter,chapter,indent,indent,left];
i need to find indexes of duplicates and also non duplicate elements .
how to do this...........give some sample code or logic......
thanks in advance
iam using objective c.....
NSArray *myWords = [string componentsSeparatedByString:#"class=\""];
int count_var=[myWords count];
tmp1=#"";
for(int i=1;i<count_var;i++)
{
str=[NSString stringWithFormat:#"\n%#",[myWords objectAtIndex:i]];
class=[str componentsSeparatedByString:#"\""];
NSString *tmp=[NSString stringWithFormat:#"%#",[class objectAtIndex:0]];
tmp1=[[NSString stringWithFormat:#"%#",tmp1] stringByAppendingString:[NSString stringWithFormat:#"%#",tmp]];
}
t1.editable=NO;
t1.text=tmp1;
NSArray *tempo=[[NSArray alloc]init];
tempo=[tmp1 componentsSeparatedByString:#"\n"];
tempCount=[tempo count];
this is my sample code...in this the array tempo contains all objects from that array i want to get index of duplicate stringsā„.
You could build a dictionary mapping the objects to index sets. For every index set, a -count of 1 means no duplicates, > 1 means there are duplicates.
NSArray *arr = [NSArray arrayWithObjects:...];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSUInteger i=0; i<[arr count]; ++i) {
id obj = [arr objectAtIndex:i];
NSMutableIndexSet *ids = [dict objectForKey:obj];
if (!ids) {
ids = [NSMutableIndexSet indexSet];
[dict setObject:ids forKey:obj];
}
[ids addIndex:i];
}
NSLog(#"%#", dict);