Better way to convert NSArray of NSString to NSArray of NSNumber - ios

Is there is a better way to convert NSArray of NSString into NSArray of NSNumber than the following code:
-(NSArray*) convertStringArrayToNumberArray:(NSArray *)strings {
NSMutableArray *numbers = [[NSMutableArray alloc] initWithCapacity:strings.count];
for (NSString *string in strings) {
[numbers addObject:[NSNumber numberWithInteger:[string integerValue]]];
}
return numbers;
}

-(NSArray *)convertStringArrayToNumberArray:(NSArray *)strings {
return [strings valueForKeyPath:#"self.integerValue"];
}
I test
NSArray * array = #[#"1",#"2",#"3"];
NSArray *num = [self convertStringArrayToNumberArray:array];
And result

Related

Split a single NSMutableArray into two so that I can set each in each of the section in UITableView

I want to use multiple sections feature UITableView. I have a single NSMutableArray which consists of data in dictionary format. In that dictionary there is a key which has values as '0' and '1'.
I want to create two separate NSMutableArray out of that so that I can assign them accordingly in different sections.
For example :
if (indexPath.section==0) {
NSDictionary *Data = [roomList1 objectAtIndex:indexPath.row];
} else {
NSDictionary *Data = [roomList2 objectAtIndex:indexPath.row];
}
Assuming the value in your dictionary is always set you could do something like:
NSMutableArray *firstArray = [NSMutableArray new];
NSMutableArray *secondArray = [NSMutableArray new];
for (NSDictionary *dictionary in yourArray) {
if ([dictionary objectForKey:#"yourValue"] isEqualToString: #"0") {
[firstArray addObject:dictionary];
} else if ([dictionary objectForKey:#"yourValue"]isEqualToString: #"1") {
[secondArray addObject:dictionary];
}
}
You can use this
- (NSDictionary *)groupObjectsInArray:(NSArray *)array byKey:(id <NSCopying> (^)(id item))keyForItemBlock
{
NSMutableDictionary *groupedItems = [NSMutableDictionary new];
for (id item in array) {
id <NSCopying> key = keyForItemBlock(item);
NSParameterAssert(key);
NSMutableArray *arrayForKey = groupedItems[key];
if (arrayForKey == nil) {
arrayForKey = [NSMutableArray new];
groupedItems[key] = arrayForKey;
}
[arrayForKey addObject:item];
}
return groupedItems;
}
Ref: Split NSArray into sub-arrays based on NSDictionary key values
Use like this
NSMutableArray * roomList1 = [[NSMutableArray alloc] init];
NSMutableArray * roomList2 = [[NSMutableArray alloc] init];
for(int i = 0;i< YourWholeArray.count;i++)
{
if([[[YourWholeArray objectAtIndex:i] valueForKey:#"YourKeyValueFor0or1"] isEqualToString "0"])
{
[roomList1 addObject:[YourWholeArray objectAtIndex:i]];
}
else if([[YourWholeArray objectAtIndex:i] valueForKey:#"YourKeyValueFor0or1"] isEqualToString "1"])
{
[roomList2 addObject:[YourWholeArray objectAtIndex:i]];
}
}
NSMutableArray *roomList1 = [[NSMutableArray alloc] init];
NSMutableArray *roomList2 = [[NSMutableArray alloc] init];
for (NSString *key in [myDictionary allKeys]) {
if (myDictionary[key] isEqualToString: #"0") {
[roomList1 addObject: myDictionary[key]];
} else {
[roomList2 addObject: myDictionary[key]];
}
}
try this way :-
NSMutableArray *getdata=[[NSMutableArray alloc]init];
getdata=[results objectForKey:#"0"];
NSMutableArray *getdata2=[[NSMutableArray alloc]init];
getdata2=[results objectForKey:#"1"];
use this two array and populate the section with now of rows at indexpathrow
define number of section 2
if (section==0){
getdata
}
else{
getdata2
}

iOS - How to get first charater of string from NSMutableArray and store it in NSMutableDictionary?

- (void)loadData: (NSMutableArray *)myMutableArray {
self.myMutableDict = [[NSMutableDictionary alloc] init];
for (NSString *str in myMutableArray) {
NSString *key = [str substringToIndex:1];
NSLog(#"Key: %#", key);
NSLog(#"str: %#", str);
if ([self.myMutableDict objectForKey:key]) {
NSMutableArray *list = (NSMutableArray *)[self.myMutableDict objectForKey:key];
[list addObject:str];
[self.myMutableDict setObject:list forKey:key];
} else {
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:str, nil];
[self.myMutableDict setObject:list forKey:key];
}
}
[self.tblAlphabeticalOrder reloadData];
}
Your mutable array has only one very long string with lots of commas in it. You need to quote each item separately:
NSArray *array = [[NSArray alloc] initWithObjects:#"Shyam", #"Ram", #"Hari", #"Gita", .....

What is the best way to convert an array with NSStrings to NSDecimalNumber?

I have an NSArray with 10 NSStrings inside:
myArray (
#"21.32",
#"658.47",
#"87.32"...
)
What's the best way to convert all the strings to an NSDecimalNumber?
A very simple Category on NSArray will allow you to use map as seen in other languages
#interface NSArray (Functional)
-(NSArray *)map:(id (^) (id element))mapBlock;
#end
#implementation NSArray (Functional)
-(NSArray *)map:(id (^)(id))mapBlock
{
NSMutableArray *array = [#[] mutableCopy];
for (id element in self) {
[array addObject:mapBlock(element)];
}
return [array copy];
}
#end
Now you can use -map: in your case like
NSArray *array = #[#"21.32",
#"658.47",
#"87.32"];
array = [array map:^id(NSString *element) {
return [NSDecimalNumber decimalNumberWithString:element];
}];
A NSMutableArray in-place variant could be
#interface NSMutableArray (Functional)
-(void)map:(id (^) (id element))mapBlock;
#end
#implementation NSMutableArray (Functional)
-(void)map:(id (^)(id))mapBlock
{
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
self[idx] = mapBlock(obj);
}];
}
#end
NSMutableArray *array = [#[#"21.32",
#"658.47",
#"87.32"] mutableCopy];
[array map:^id(NSString *element) {
return [NSDecimalNumber decimalNumberWithString:element];
}];
It would be quite simple with a for loop:
myArray (
#"21.32",
#"658.47",
#"87.32"...
)
NSMutableArray *numberArray = [NSMutableArray arrayWithCapacity: myArray.count];
for (aString in myArray)
{
NSDecimalNumber *aNumber = [NSDecimalNumber decimalNumberWithString: aString];
[numberArray addObject: aNumber];
}

How to remove NSDictionary while iterating in NSMutableArray [duplicate]

This question already has answers here:
Avoiding "NSArray was mutated while being enumerated"
(11 answers)
Closed 7 years ago.
I want to remove object from array when the name is same. How to do this? Thanks in advance. Here is my code
NSString *name1;
NSString *name2;
NSArray *copyArray = [uniqueArraySort copy];
for (NSMutableDictionary *dict1 in copyArray) {
for (NSDictionary *dict2 in uniqueArraySort) {
name1 = [dict1 valueForKey:#"name"];
name2 = [dict2 valueForKey:#"name"];
if ([name1 isEqualToString:name2]) {
NSLog(#"%#",uniqueArraySort);
[uniqueArraySort removeObject:dict2];
NSLog(#"%#",uniqueArraySort);
}
}
}
And when I compile I get this error
<__NSArrayM: 0x7fea39804f40> was mutated while being enumerated.'
Try something like this... I assume value for key "name" might be a string.
NSSet *set = [NSSet setWithArray:[uniqueArraySort valueForKey:#"name"]];
NSMutableArray *array = [NSMutableArray new];
for (NSString* value in set.allObjects) {
NSArray *filteredArray = [uniqueArraySort filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"name = %#", value]];
if(filteredArray.count > 0)
[array addObject:[filteredArray firstObject]];
}
return array;
UPDATE : On second thought, I found that code above is still too much. You can do it at O(n) using hashtable.
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:uniqueArraySort.count];
for(NSDictionary *item in uniqueArraySort)
{
NSString *name = [item objectForKey:#"name"];
if([dictionary objectForKey:name] == nil)
{
[dictionary setObject:item forKey:name];
}
}
return dictionary.allValues;
What you need to do is create a separate mutable array. Something like this:
NSMutableArray *itemsToDelete = [NSMutableArray array];
NSMutableArray *copyArray = [uniqueArraySort mutableCopy];
for (NSMutableDictionary *dict1 in copyArray) {
for (NSDictionary *dict2 in uniqueArraySort) {
name1 = [dict1 valueForKey:#"name"];
name2 = [dict2 valueForKey:#"name"];
if ([name1 isEqualToString:name2]) {
NSLog(#"%#",uniqueArraySort);
[itemsToDelete addObject:dict2];
NSLog(#"%#",uniqueArraySort);
}
}
}
[copyArray removeObjectsInArray:itemsToDelete];
Also, I'm not sure what you are doing with uniqueArraySort but you might need to make that an NSMutableArray if you plan on using it after.

Exception "mutating method sent to immutable object"

I Had an exception in the line [array removeObjectsInArray:toRemove]; in the method below and can't understand what's wrong with it..
- (void) handleDearchForTerm:(NSString *)searchTerm
{
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in _keys)
{
NSMutableArray *array = [_names valueForKey:key];
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (NSString *name in array)
{
if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
[toRemove addObject:name];
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
}
[_keys removeObjectsInArray:sectionsToRemove];
[_table reloadData];
}
Probably array is just instance of NSArray, but not NSMutableArray, you shall check _names setObject:forKeys:
Change:
NSMutableArray *array = [_names valueForKey:key];
To:
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[_names valueForKey:key]];
Or:
NSMutableArray *array = [[_names valueForKey:key] mutableCopy];

Resources