how to find different object in NSMutableArray and add to another NSMutableArray? - ios

I have one problem and I want to search every object in my NSMutableArray.
if this object (the object for searching) no "0" add to another NSMutableArray with name : StarArray
this is my first NSMutableArray
(
(
1,
1,
6
),
(
0
),
(
0,
3
),
(
5,
3
),
(
0
)
)
this NSMutableArray is 2d (matrix) and I want get every object except object that have 0 value and add to StarArray
StarArray : {1,1,6,3,5,3}

Try this
It will take less iterations to execute
NSMutableArray *resultArr = [NSMutableArray array];
for(NSArray *arr in yourArr)
{
NSMutableArray *ar = [NSMutableArray arrayWithArray:arr];
while( [ar containsObject:#"0"])
{
[ar removeObject:#"0"];
}
if([ar count])
[resultArr addObjectsFromArray:ar];
}

Try this code, it may work:
NSMutableArray *StarArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [firstArray count] ; i++) {
for (id object in [firstArray objectAtIndex:i]) {
if (![object isEqualToString:#"0"]) {
[StarArray addObject:object];
}
}
}
NSLog(#"%#", StarArray);

Related

iOS:How to separate particular string element in array?

I got following response from server:
[{"bp":"000/000","dateTime":"05/12/2016 01:02:59 PM","doc":{"email_id":"batra#gmail.com","exception":0,"gender":"Male","id":0,"mobile_no":8055621745,"name":"Batra","profile_id":0,"qualification":"MD(Doctor)","reg_id":157,"salutation":"Mr","wellness_id":"251215782521"},"follow_up":"17","id":37,"medicine":["Syrup,Decold Total,20,0-0-1,Before Meal,1","Injection,Insulin,1,0-0-1,Before Meal,1","no","no","no","no","no","no","no","no"],"patient":{"email_id":"bishtrohit1989#gmail.com","exception":0,"gender":"Male","id":0,"mobile_no":8055621745,"name":"Rohit","profile_id":0,"qualification":"","reg_id":150,"salutation":"Mr","wellness_id":"290119935030"},"weight":"000"}]
From that I have separate the medicine array like following way:
NSMutableArray *Myarray = [NSMutableArray new];
for (int i=0; i<_menuItems.count; i++) {
[Myarray addObject:[[_menuItems objectAtIndex:i] objectForKey:#"medicine"]];
NSLog(#"medicine: %#",Myarray);
I got output for this as like:
medicine: (
(
"Syrup,Decold Total,20,0-0-1,Before Meal,1",
"Injection,Insulin,1,0-0-1,Before Meal,1",
no,
no,
no,
no,
no,
no,
no,
no
)
)
Now what i want:
1) remove that all noelement.
2) after that, i want only 2nd element in each string.
in short i want my final output is like:
[Decold Total, Insulin];
But i don't know how to do that..??
Please anyone can solve my issue. help will be appreciable.
You need to use NSPredicate on Myarray and filter it.
Make your Myarray like this.
NSMutableArray *Myarray = [NSMutableArray new];
for (int i=0; i<_menuItems.count; i++) {
[Myarray addObjectsFromArray:[[_menuItems objectAtIndex:i] objectForKey:#"medicine"]];
}
1) Remove that all no element.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"NOT (SELF = %#)",#"no"];
NSArray *filterArray = [Myarray filteredArrayUsingPredicate:predicate];
2) Want only 2nd element in each string
NSMutableArray *medicineArray = [[NSMutableArray alloc] init];
for (NSString* medicine in filterArray) {
NSArray *arr = [medicine componentsSeparatedByString:#","];
if (arr.count >= 2) {
[medicineArray addObject:[arr objectAtIndex:1]];
}
}

Matching Mutable Array value to index of another Array and getting other Array value at that index

Objective: compare values stored in Mutable array(which are changing constantly) to the index of a static Array. When the values match I would like to add the object(s) of the static array to a new mutable array.
Example:
Values of MutableArray at present state:
(1,2,3)
(Index) - Values of Static Array:
(0) - "Zero"
(1) - "One"
(2) - "Two"
(3) - "Three"
(4) - "Four"
Take values from initial MutableArray and compare them to the static array index's. Output the values at those index's to a new MutableArray
("One","Two","Three")
The initial array is derived by passing the MutableArrayContents for a search via predicate into this:
NSMutableArray *indexArray = [NSMutableArray array];
for (NSMutableArray* obj in _searchResults)
{
if([_questionsArray containsObject:obj] && ![indexArray containsObject:obj])
[indexArray addObject:#([_questionsArray indexOfObject:obj])];
}
IndexArray is the array of "index's".
I've tried for loops but they are not working correctly, any help would be appreciated.
Thanks!
You can try this:
NSArray *staticArr = #[#"Zero",#"One",#"Two",#"Three",#"Four"];
NSArray *otherArr = #[#1,#2,#3];
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:[otherArr count]];
for(int i = 0; i < [staticArr count]; i++) {
for(int j = 0; j < [otherArr count]; j++) {
if([[otherArr objectAtIndex:j] intValue] == i) {
[array insertObject:[staticArr objectAtIndex:i] atIndex:j];
break;
}
}
}
Try this method
NSArray *staticArr = #[#"Zero", #"One", #"Two", #"Three", #"Four"];
NSArray *otherArr = #[#(1), #(2), #(3)];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[otherArr count]];
[staticArr enumerateObjectsUsingBlock:^(NSString *string, NSUInteger staticIndex, BOOL *staticStop) {
[otherArr enumerateObjectsUsingBlock:^(NSNumber *number, NSUInteger index, BOOL *stop) {
if (number.integerValue == staticIndex) {
[array insertObject:string atIndex:index];
}
}];
}];
I am typing this on a tab. Please forgive any formatting issues
NSMutableArray *indices; //I am omitting the adding of data, it contains required indices as NSNumber objects
NSArray *staticArr = #[#"One", #"Two", #"Three", #"Four", #"Five"];
NSMutableArray *array = [NSMutableArray new];
for(NSNumber *index in indices) {
NSInteger idx = [index integerValue];
If(idx < staticArr.count)
{
[array addObject:[staticArr objectAtIndex:idx];
}
}
This will be in the order of the elements in indices array and may have repetitions in case there are repetitions in indices array.
This is the most straightforward way, there are many ways to get the subarray using Apple's API

How to access Array within Array within Array while dealing with UITableView

Here is the NSLog of my Structure (Each enum is a custom tableViewCell). I need to be able to show each cell in my table view.-
(
(
0,
0
),
(
4
),
(
1,
(
(
2
),
(
2
),
(
2
),
(
2
),
(
2
),
(
2
),
(
2
),
(
2
)
)
),
(
4
)
)
As you can see, it is an Array with 4 Array objects. Within these objects are enums, or arrays with enums. [0] has an array with two 0 enums, [1] has an array with one 4 enum, [2] has an array with one 1 enum, AND an array of arrays within that has eight enums of 2.
My question is how do I display these cells in
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BucketListCellType type = [[[[self.presentedCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:0] intValue];
}
is getting me an error - [__NSCFNumber objectAtIndex:]: unrecognized selector sent to instance
Here is where I am constructing that array -
-(void)showPresentedCells
{
NSMutableArray *preCells = [NSMutableArray new];
// First Section - Suggested Bucket List Images
NSMutableArray *suggestedBucketListImages = [NSMutableArray new];
[suggestedBucketListImages addObject:#(mainPhotoCellWithAddToBucketList)];
[suggestedBucketListImages addObject:#(mainPhotoCellWithAddToBucketList)];
[preCells addObject:[NSArray arrayWithArray:suggestedBucketListImages]];
// Second Section - Top Bucket List Images
NSMutableArray *topBucketListImages = [NSMutableArray new];
[topBucketListImages addObject:#(mainPhotoCell)];
[preCells addObject:[NSArray arrayWithArray:topBucketListImages]];
// Third Section - My Bucket List Images
NSMutableArray *myBucketListCategories = [NSMutableArray new];
NSMutableArray *categoryWithPictures = [NSMutableArray new];
[myBucketListCategories addObject:#(CreateNewBucketListCell)];
for (int i = 0; i <= [self.bucketListCategories count]; i++) {
[categoryWithPictures addObject:#[#(CategoryCell)]];
}
[myBucketListCategories addObject:[NSArray arrayWithArray:categoryWithPictures]];
[preCells addObject:[NSArray arrayWithArray:myBucketListCategories]];
NSMutableArray *unorganizedBucketListImages = [NSMutableArray new];
[unorganizedBucketListImages addObject:#(mainPhotoCell)];
[preCells addObject:[NSArray arrayWithArray:unorganizedBucketListImages]];
self.presentedCells = preCells;
}
Thanks for any help.
The source of the problem is that you're doing calling objectAtIndex for three levels of arrays, but that will work only where you really have three levels of structure (e.g. section 2, row 1). Everywhere else you only have two levels of structure (returning a NSNumber) and trying to call objectAtIndex with that will yield the error you describe.
What's less clear to me is what you want to show when there is only two levels of structure. But you could presumably do something like:
BucketListCellType type;
id object = self.presentedCells[indexPath.section][indexPath.row];
if ([object isKindOfClass:[NSArray class]]) {
type = [object[0] intValue];
} else {
type = [object intValue];

Append an object in an array

I want to append an object in an array.
For example,
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<5; i++)
{
[array addObject:#[#"Any"]];
}
It gives an output like this:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any
),
(
Any
)
)
Now I want to append object at index 3 of an array so that it could appear like below:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any, Where, How, When
),
(
Any
)
)
Use function insertObjectAtIndex to achieve it.
[array insertObject:anObject atIndex:2];
If I understand you...
[array replaceObjectAtIndex:2 withObject:#[[[array objectAtIndex:2] firstObject],#"Where",#"How",#"When"]];
Try this:
NSMutableArray *array = [NSMutableArray new];
for (int i=0; i<5; i++)
{
[array addObject:[NSMutableArray arrayWithObject:#"Any"]];
}
NSLog(#"%#", array);
NSMutableArray *subArray = [array objectAtIndex:3];
[subArray addObjectsFromArray:#[#"Where", #"How", #"When"]];
NSLog(#"%#", array);
Note, #[] creates an instance of NSArray class which cannot be modified later. You can modify only instances of NSMutableArray class.

How can I add elements to an NSDictionary in the following format?

I have an NSArray of names and ages. Now I am trying to create a new NSDictionary with item 0 holding the first name and first age and item 1 holding the second name and second age from the corresponding NSArray? Is that possible?
In my viewDidLoad:
NSMutableArray *Names = [[NSMutableArray alloc] init];
NSMutableArray *ages = [[NSMutableArray alloc] init];
for(int i = 0; i < 4; i++) {
[Names addObject:[candidates objectAtIndex:i]];
[ages addObject:[studenAge objectAtIndex:i]];
}
But how can I make an NSDictionary from this by order?
Final result
I want to write this NSDictionary into a .plist so it look like this:
Details
{
item 0
{
name:rahul
age:25
}
item 1
{
name:ram
age:26
}
item 2
{
name:aajy
age:20
}
item 4
{
name:raj
age:25
}
}
Dictionaries do not have an order.
Probably you want to add dictionaries to the array:
NSMutableArray *persons=[[NSMutableArray alloc]init];
for(int i=0;i<4;i++)
{
[persons addObject:# { #"name" : candidate[i], #"age" : studenAge[i] }];
}
You can sort this array with the sort-Methods of NSArray, NSMutableArray.
As mentioned by #Amin, dictionaries don't maintain order and since you seem to want an indexed access here is what I suggest as the final structure: an array of dictionaries:
NSMutableArray *details = [NSMutableArray array];
for (int i=0; i<4; i++) {
[details addObject:#{#"name": canditate[i], #"age":studentAge[i]}];
}
This will give you the following structure:
[{name:rahul, age:25}, {name:ram age:26},...]

Resources