how to copy the required Items of NSMutableArray to NSArray [duplicate] - iphone

This question already has answers here:
How to split an NSArray into two equal pieces?
(3 answers)
Closed 9 years ago.
I have NSMutableArray of Results it has 6 items. I want to copy the first three contents
to another NSArray.

Just use subarrayWithRange::
NSMutableArray *oldArray = ... // the mutable array with the 6 objects
NSArray *result = [oldArray subarrayWithRange:NSMakeRange(0, MIN(3, oldArray.count))];

NSMutalbeArray *newArray=[[NSMutableArray alloc] init];
for(int i=0; i < results.count; i++){
[newArray addObject:results[i]];
if(i == 2)
break;
}

Try this simple loop:
NSMutableArray *resultantArray=[NSMutableArray new];
for(NSInteger i=0;i<3;i++){
[resultantArray addObject:firstArray[i]];
}

Related

How to concatenate all values of array in Xcode [duplicate]

This question already has answers here:
Join an Array in Objective-C
(3 answers)
Closed 9 years ago.
I am new in iOS and i am not able to find solution of my question . So please tell me how can i concatenate all values of array in iOS
Answer : -componentsJoinedByString: .
It will join the components in the array by the specified string and return a string representation of the array.
Use : NSString *finalStr = [yourArray componentsJoinedByString:#" "];
GoodLuck !!!
if you are using string values in NSArray then it is quite simple.
You can just run for loop and use a NSMutableString to append the data.
NSArray *_array;//Your Initialized Array
NSMutableString *_string =[[NSMutableString alloc]init];
for(int i= 0 ;i<[_array count];i++)
{
[_string appendFormat:#"%#",[_array objectAtIndex:i]];
}
NSLog(#"_string = %#",_string);
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<[array count];i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
NSLog(#"mystirng=%#", theString;);

Remove certain objects from NSMutableArray [duplicate]

This question already has answers here:
Removing object from NSMutableArray
(5 answers)
Closed 9 years ago.
I have an NSMutableArray of objects which are of AdDetail class that hold a few properties (for eg. adId, adTitle, adPrice... etc). I want to remove only those objects which have adID = 0. How can I do that ?
Perhaps something more elegant would suffice?
[array removeObjectsInArray:[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"adID == 0"]]];
Using predicate
NSArray *filtered=[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(adId == 0)"]];
Using fastEnumeration:
NSMutableArray *newArray=[NSMutableArray new];
for(AdDetail adDetailObj in array){
if(![[adDetailObj adId] isEqualToString:#"0"]){ //if these are strings, if NSInteger then directly compare using ==
newArray[newArray.count]=adDetailObj;
}
}
Now newArray contains all objects other than id=0
Use following code :
int count = array.count;
for(i=0;i<count;i++){
ADetail *adetail = [array objectAtIndex:i];
if(adetail.adID = 0){
[array removeObjectAtIndex:i];
i--;
}
count = array.count;
}
NSMutableArray *newArray = [NSMutableArray arrayWithArray:yourArray];
for (int i = 0; i < yourArray.count; i++)
{
AdDetail *obj = (AdDetail *)[yourArray objectAtIndex:i];
if (obj.adID == 0)
[newArray removeObjectAtIndex:i];
}
yourArray = [newArray mutableCopy];
for(i=0; i < myArray.count; i++)
{
myClass = [myArray objectAtIndex:i];
if([myClass.adID isEqualtoString:"0"])// if it it int/NSInteger the write myClass.adID==0
{
[myArray removeObjectAtIndex:i];
i--;
}
}
predicate = #"adID == 0";
newArray = [theArray filterUsingPredicate:aPredicate]

How to get last array values from NsmutableArray [duplicate]

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

how is the code dividing single array into multiple arrays? [duplicate]

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

how to converts NSMutableArray values into NSString? [duplicate]

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");
}
}