iOS pass the value instead of reference - ios

When executing the below block. My friends_dict NSMutableDictionary value changes as friend changes. If I'm not wrong this is because the reference of the value in the dictionary is passed along to the friend. How can I pass the value instead of reference and prevent the dictionary from changing.
NSMutableArray *friendsArray = [[NSMutableArray alloc] initWithArray:[friends_dict valueForKey:selectedFriendId]];
for (NSObject *object in [response objectForKey:#"friend_nearby"]){
NSString *id = [NSString stringWithFormat:#"%#",[object valueForKey:#"id"]];
NSString *spotValue = [NSString stringWithFormat:#"%#",[object valueForKey:#"spot"]];
for(int i = 0; i < [friendsArray count]; i++){
FriendDataModel *friend = [[FriendDataModel alloc] init];
friend = [friendsArray objectAtIndex:i];
if ([friend.friendId isEqualToString:id] && ![friend.spot isEqualToString:spotValue] ) {
friend.spot = spotValue; //This is where the dictionary value changes
[friendsArray replaceObjectAtIndex:i withObject:friend];
spotChanged = YES;
}
}
}

Copy the array and pass in the copy.
NSArray *friendList = [friends_dict valueForKey:selectedFriendId];
NSMutableArray *friendsArray = [friendList mutableCopy];

Related

How to Insert data into NSMutableArray that is inside NSMutableDictionary inside NSMutableArray?

I have an NSMutableArray that contains several NSMutableDictionary object, each dict has a NSString and an NSMutableArray in it.
My problem is that I need to find the correct dict based on a string match and when found insert an object into the NSMutableArray in the same dict where the string match was found, I can only make this work the first time when the array is empty because after that I am unable to match the correct string:
Here is what I have tried so far, I tried using contains object but that wont get me inside the dict inside the array so I am stuck
NSMutableArray *variantOptions = [[NSMutableArray alloc] init];
for (NSDictionary *variant in variantInfo) {
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [[NSMutableArray alloc] init];
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
if(variantOptions.count > 0) {
// Need to loop through the array until name isEquaToString variantOptionName and when true insert value into variantOptionValuesArray in that same dict and if not exists create a new dict and insert in array
} else {
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
}
[variantOptions addObject:variantOption];
}
for (NSDictionary *variant in variantInfo) {
NSString *name = variant[#"name"];
NSString *value = variant[#"value"];
BOOL found = false;
for (NSMutableDictionary *v in variantOptions) {
if (v[#"name"] isequalToString:name]) {
NSMutableArray *values = v[#"variantOptionValues"];
[values addObject:value];
found = true;
break;
}
}
if (!found) {
// name was not found
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [NSMutableArray alloc] init];
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:#"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:#"variantOptionValues"];
[variantOptions addObject:variantOption];
}
}

Remove duplicates from NSArray case insensitively using NSSet

NSArray*arr = #[#"ram",#"Ram",#"vinoth",#"kiran",#"kiran"];
NSSet* uniqueName = [[NSSet alloc]initWithArray:arr];
NSLog(#"Unique Names :%#",uniqueName);
Output:
but i need the output as
You could first convert them all to lowercase strings.
NSArray *arr = #[#"ram",#"Ram",#"vinoth",#"kiran",#"kiran"];
NSArray *lowerCaseArr = [arr valueForKey:#"lowercaseString"];
NSSet* uniqueName = [[NSSet alloc] initWithArray:lowerCaseArr];
NSLog(#"Unique Names :%#",uniqueName);
Unique Names :{(
ram,
kiran,
vinoth
)}
Try this:
NSArray *arr = [NSArray arrayWithObjects:#"Ram",#"ram", nil]; //this is your array
NSMutableArray *arr1 = [[NSMutableArray alloc]init]; //make a nsmutableArray
for (int i = 0; i<[arr count]; i++) {
[arr1 addObject:[[arr objectAtIndex:i]lowercaseString]];
}
NSSet *set = [NSSet setWithArray:(NSArray*)arr1];//this set has unique values
This will always preserve casing form that was existing in your original container (although it's undefined which casing):
NSArray<NSString*>* input = ...
NSMutableDictionary* tmp = [[NSMutableDictionary alloc] init];
for (NSString* s in input) {
[tmp setObject:s forKey:[s lowercaseString]];
}
return [tmp allValues];
Create a mutable array the same size as arr. Fill it with lowercaseString versions of each element of arr. Make the set out of that.
#Updated
Using this you remove uppercase string from your array.
NSMutableArray *arr= [[NSMutableArray alloc]initWithObjects:#"ram",#"Ram",#"vinoth",#"kiran", nil];
NSMutableArray *arrCopy = [[NSMutableArray alloc]init];
for (int index = 0 ; index<arr.count; index++) {
NSUInteger count = [[[[arr objectAtIndex:index] componentsSeparatedByCharactersInSet:[[NSCharacterSet uppercaseLetterCharacterSet] invertedSet]] componentsJoinedByString:#""] length];
if (count == 0) {
[arrCopy addObject:[arr objectAtIndex:index]];
}
}
NSLog(#"Print Mutable Copy %#",arrCopy);
try this one
NSArray *copyArray = [mainArray copy];
NSInteger index = [copyArray count] - 1;
for (id object in [copyArray reverseObjectEnumerator]) {
if ([mainArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
[mainArray removeObjectAtIndex:index];
}
index--;
}
copyArray=nil;

Sort NSMutableDictionnary from Plist in UICollectionView

I have a uicollectionView which display items from dictionaries (contained in a main dictionary in a plist). I need to sort it with the key "order" which is inside my dictionaries. I can't figure how to do this. I try to compare keys without success using descriptors.
I take the values from nsmutablearrays to display them in the cell.
Here my code (the one thart working unsorted):
//To retreive the data from the plist
NSMutableDictionary *items = [[NSMutableDictionary alloc] initWithContentsOfFile: finalPath];
//initialize arrays
_order = [[NSMutableArray alloc]initWithCapacity:10];
_name = [[NSMutableArray alloc]initWithCapacity:10];
_role = [[NSMutableArray alloc]initWithCapacity:10];
_image = [[NSMutableArray alloc]initWithCapacity:10];
_description = [[NSMutableArray alloc]initWithCapacity:10];
_email = [[NSMutableArray alloc]initWithCapacity:10];
_googleplus = [[NSMutableArray alloc]initWithCapacity:10];
//load every NSDictionnary on the view
NSMutableDictionary *loadedItem = [[NSMutableDictionary alloc]initWithCapacity:1];
NSMutableDictionary *theMember = [[NSMutableDictionary alloc]initWithCapacity:10];
loadedItem = [items objectForKey:#"Team"];
for (int i = 0; i < loadedItem.count; i++) {
theMember = loadedItem.allValues[i];
NSString *memberOrder = [theMember objectForKey:#"order"];
[_order addObject:memberOrder];
NSLog(#"member order = %#", _order);
NSString *memberName = [theMember objectForKey:#"name"];
[_name addObject:memberName];
NSLog(#"member name = %#", _name);
NSString *memberRole = [theMember objectForKey:#"role"];
[_role addObject:memberRole];
NSLog(#"member role = %#", _role);
NSString *memberImage = [theMember objectForKey:#"image"];
[_image addObject:memberImage];
NSLog(#"member image = %#", _image);
NSString *memberDesc = [theMember objectForKey:#"description"];
[_description addObject:memberDesc];
NSLog(#"member description = %#", _description);
NSString *memberMail = [theMember objectForKey:#"email"];
[_email addObject:memberMail];
NSLog(#"member mail = %#", _email);
NSString *memberGoogleplus = [theMember objectForKey:#"googleplus"];
[_googleplus addObject:memberGoogleplus];
NSLog(#"member googleplus = %#", _googleplus);
}
[self.collectionView reloadData];
}
I dealt with this by changing my mai ndict into an array which is better to manipulate.

Grouping the NSArray elements into NSMutableDictionary

I have an NSArray some thing like in the following format.
The group array is :
(
"Q-1-A1",
"Q-1-A9",
"Q-2-A1",
"Q-2-A5",
"Q-3-A1",
"Q-3-A8",
"Q-4-A1",
"Q-4-A4",
"Q-10-A2",
"Q-8-A2",
"Q-9-A2",
"Q-7-A1",
"Q-5-A2"
)
Now what i have to do is group the array elements some thing like this.
1 = ( "Q-1-A1","Q-1-A9")
2 = ("Q-2-A1","Q-2-A5",) ...
10 =("Q-10-A2")
can any one please help me how can i achieve this.
Thanks in advance.
Try
NSArray *array = #[#"Q-1-A1",
#"Q-1-A9",
#"Q-2-A1",
#"Q-2-A5",
#"Q-3-A1",
#"Q-3-A8",
#"Q-4-A1",
#"Q-4-A4",
#"Q-10-A2",
#"Q-8-A2",
#"Q-9-A2",
#"Q-7-A1",
#"Q-5-A2"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSString *string in array) {
NSArray *components = [string componentsSeparatedByString:#"-"];
NSString *key = components[1];
NSMutableArray *tempArray = dictionary[key];
if (!tempArray) {
tempArray = [NSMutableArray array];
}
[tempArray addObject:string];
dictionary[key] = tempArray;
}
Create an NSMutableDictionary, then iterate through your 'group array'.
For each NSString object:
get the NSArray of componentsSeparatedByString:#"-"
use the second component to create a key and retrieve the object for that key from your mutable dictionary. If its nil then set it to an empty NSMutableArray.
add the original NSString to the mutable array.
Try this
NSArray *arrData =[[NSArray alloc]initWithObjects:#"Q-1-A1",#"Q-1-A9",#"Q-2-A1",#"Q-2-A5",#"Q-3-A1",#"Q-3-A8",#"Q-4-A1",#"Q-4-A4",#"Q-10-A2",#"Q-8-A2",#"Q-9-A2",#"Q-7-A1",#"Q-5-A2", nil ];
NSMutableDictionary *dictList = [[NSMutableDictionary alloc]init];
for (int i=0; i<[arrData count];i++) {
NSArray *arrItem = [[arrData objectAtIndex:i] componentsSeparatedByString:#"-"];
NSMutableArray *arrSplitedItems = [dictList valueForKey:[arrItem objectAtIndex:1]];
if (!arrSplitedItems) {
arrSplitedItems = [NSMutableArray array];
}
[arrSplitedItems addObject:[arrData objectAtIndex:i]];
[dictList setValue:arrSplitedItems forKey:[arrItem objectAtIndex:1]];
}
NSArray *sortedKeys =[dictList allKeys];
NSArray *sortedArray = [sortedKeys sortedArrayUsingComparator:^(id str1, id str2) {
return [((NSString *)str1) compare:((NSString *)str2) options:NSNumericSearch];
}];
for (int i=0; i<[sortedArray count]; i++) {
NSLog(#"%#",[dictList objectForKey:[sortedArray objectAtIndex:i]]);
}
listOfYourMainArray/// Its YOur main Array;
temArray = (NSArray *)listOfYourMainArray; // Add Your main array to `temArray`.
NSMutableDictionary *lastDic = [[NSMutableDictionary alloc] init]; /// YOu need to creat Dictionary for arrange your values.
for (int i = 0; i< listOfYourMainArray.count; i++)
{
for (int j = 0 ; j < temArray.count; j ++)
{
if (([[temArray objectAtIndex:j] rangeOfString:[NSString stringWithFormat:#"Q-%d", i] options:NSCaseInsensitiveSearch].location != NSNotFound))
{
[lastDic setValue:[temArray objectAtIndex:j] forKey:[NSString stringWithFormat:#"%d", i]];
}
}
}
NSLog(#"%#", lastDic)

how to retrieve a NSArray from a custom class

I have a custom class which has a integer as a variable.
// addons.h
-(NSMutableArray *) goodDirections:(int)iNumber;
// addons.m
-(NSMutableArray *) goodDirections:(int)iNumber;
{
NSString *gOne = #"one"+iNumber;
NSString *gTwo = #"two"+iNumber;
NSString *gThree = #"three"+iNumber;
NSString *gFour = #"four"+iNumber;
NSMutableArray *goodValues = [NSMutableArray arrayWithObjects:gOne,gTwo,gThree,gFour,nil];
return goodValues;
}
// ViewController.m
addons *directions =[[addons alloc]init];
NSMutableArray *helloTest = [[NSMutableArray alloc]init];
helloTest = [directions goodDirections:3];
NSString *obj1 = [helloTest objectAtIndex:1];
NSLog(#"%#",obj1);
and the custom class has a variable number, when entered returns an array with 4 string values, how do I retrieve the values from the array in my implementation file viewController.m
Make sure you import the custom class in the file you want.Then
NSMutableArray *arr = [theObject simpleMethod:4];
To retrieve any object in arr, you can use objectAtIndex function (for example):
NSString *item = [arr objectAtIndex:1];
please! try below code snip
NSMutableArray *arr = [theObject simpleMethod:4];
obj = [arr objectAtIndex:0];
You can use array object at index.
NSMutableArray *arrayTemp = [self simpleMthod:2];
objTemp = [arrayTemp objectAtIndex:0];
Or You can use
for (int i=0; i<[arrayTemp count]; i++)
{
objTemp = [arrayTemp objectAtIndex:i];
}

Resources